2017-02-16 7 views
0

버튼 클릭으로 바코드를 스캔하는 응용 프로그램을 만들고 있으며 롤리팝 버전까지 제대로 작동했습니다. 내가 마시맬로에 왔을 때 일하는 것이 멈췄다.런타임에 카메라 사용 권한을 요청하는 방법

camerabase an error occurred while connecting to camera 0

그것은 나에 의해 허가를 켜 강제 :이 오류입니다

설정 -> 응용 프로그램 -> 내 응용 프로그램 -> 카메라.

제 질문은 마쉬멜로 우에서 내 앱에 카메라 권한을 자동으로 허용하거나 런타임에 카메라를 켜도록 요청하는 방법입니다. 스크린 샷 : 허가

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, 
     Manifest.permission.WRITE_CALENDAR); 
권한이 부여되면

, permission check = PackageManager.PERMISSION_GRANTED 또는 그 반대의 경우도 마찬가지 PERMISSION_DENIED에 대한

enter image description here

+1

https://developer.android.com/training/permissions/requesting.html의 –

+1

가능한 복제 [안드로이드 M이 - 실행 권한을 확인하는 데 실패] (http://stackoverflow.com/questions/38121104/ android-m-failed-to-check-runtime-permission) –

+0

[Android 마시맬로 요청 권한?] (https://stackoverflow.com/questions/33666071/android-marshmallow-request-permission) – Vanna

답변

-2

먼저 확인하시기 바랍니다.

그런 다음 요청할 수 있습니다. 이 도움이 될 here

0

에서 직접 촬영

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // contacts-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

코드 :

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) 
{ 
    if(ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CAMERA)) 
    { 
     AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); 
     alertBuilder.setCancelable(true); 
     alertBuilder.setTitle("Permission necessary"); 
     alertBuilder.setMessage("CAMERA is necessary"); 
     alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
      @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
      public void onClick(DialogInterface dialog, int which) { 
       ActivityCompat.requestPermissions((Activity) context, 
       new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUESTS); 
      } 
     }); 
     AlertDialog alert = alertBuilder.create(); 
     alert.show(); 
    } else { 
     ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUESTS); 
    } 
    return false; 
} else { 
    return true; 
} 

MY_PERMISSIONS_REQUESTS이의 최종 값이다

if (ContextCompat.checkSelfPermission(thisActivity, 
       Manifest.permission.READ_CONTACTS) 
     != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
      Manifest.permission.READ_CONTACTS)) { 

     // Show an explanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
       new String[]{Manifest.permission.READ_CONTACTS}, 
       MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

는 당신은 응답을 처리해야 요청 코드.

제게 잘 작동합니다.

3
if (ContextCompat.checkSelfPermission(getContext(), 
      Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 

     if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) 
       getContext(), Manifest.permission.CAMERA)) { 


     } else { 
      ActivityCompat.requestPermissions((Activity) getContext(), 
        new String[]{Manifest.permission.CAMERA}, 
        MY_PERMISSIONS_REQUEST_CAMERA); 
     } 

    } 
+0

의 가능한 복제본 나 (마시 멜로). 이 코드를 사용해보십시오 ... –