2017-05-04 1 views
4

다음 코드를 사용하여 런타임 카메라 사용 권한을 확인하고 있는데도 내 앱에서 Camera2 API를 사용하려고합니다.Android 카메라 런타임 권한 오류가 발생 했습니까?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { 
       cameraManager.openCamera(cameraId, stateCallBack, null); 

      } else { 
       if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) 
        Toast.makeText(getApplicationContext(), "PLease allow the app to use camera app", Toast.LENGTH_LONG).show(); 

      } 
      ActivityCompat.requestPermissions(CaptureImageActivity.this,new String[]{"android.manifest.permissin.CAMERA"}, CAMERA_REQUEST_RESULT); 

     } else { 
      cameraManager.openCamera(cameraId, stateCallBack, null); 
     } 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResult) { 

    switch (requestCode) { 

     case CAMERA_REQUEST_RESULT: 
      if (grantResult[0] == PackageManager.PERMISSION_GRANTED) { 

       try { 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 
         //this method is created because of openCamera method below i don't understand why this method is created 
         return; 
        } 
        cameraManager.openCamera(cameraId, stateCallBack, null); 
       } catch (CameraAccessException e) { 
        e.printStackTrace(); 
       } 
      } 
      if (grantResult[0] != PackageManager.PERMISSION_GRANTED) 
       Toast.makeText(getApplicationContext(), "camera is not granted", Toast.LENGTH_LONG).show(); 


      break; 
     default: 
      super.onRequestPermissionsResult(requestCode, permission, grantResult); 
      break; 
    } 
} 

AndroidManifest.xml 파일에도 권한이 있습니다. 난 내 응용 프로그램을 실행할 때

<uses-permission android:name="android.permission.CAMERA" /> 

는 그러나 권한 대화 상자가 표시되지 않고, 카메라는 토스트가 보여주는 부여되지 않습니다.

1) 권한 대화 상자가 표시되지 않는 이유는 무엇입니까?

2) 카메라에 토스트가 나타나지 않는 이유는 무엇입니까? 나는 많은 것을 수색했지만 도움이되는 것은 아무것도 없다! 여기

답변

3

private static final int PERMISSIONS_REQUEST_CAPTURE_IMAGE = 1; 
if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.CAMERA) 
      != PackageManager.PERMISSION_GRANTED) { 
     // User may have declined earlier, ask Android if we should show him a reason 

     if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { 
      // show an explanation to the user 
      // Good practise: don't block thread after the user sees the explanation, try again to request the permission. 
     } else { 
      // request the permission. 
      // CALLBACK_NUMBER is a integer constants 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAPTURE_IMAGE); 
      // The callback method gets the result of the request. 
     } 
    } else { 


    } 
@Override 
    public void onRequestPermissionsResult (int requestCode, String[] permissions, 
    int[] grantResults){ 
     switch (requestCode) { 
      case PERMISSIONS_REQUEST_CAPTURE_IMAGE: { 
       if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        // permission was granted 

        Log.d("", "permission granted success"); 


       } else { 
        // permission denied 

        Log.d("", "permission denied"); 
       } 
       return; 
      } 
     } 
    } 
+0

내 코드가 코드와 유사한 카메라 API에 대한 작업 실행 권한입니다하지만이 작동하지 않습니다. – Yirga

관련 문제