0

I can't seem to get the dialog box to pop up for camera request permission. I don't see what mistake I made. I think I followed the Android Developer document on permissions well enough yet I'm still not seeing a dialog box appear for permissions. What is wrong with the code and what can I do to fix it?

 private static final int REQUEST_CAMERA_PERMISSION_RESULT = 0;


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);


    // v checks for request code
    if (requestCode == REQUEST_CAMERA_PERMISSION_RESULT){

          // v check if request was granted or not
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
            Toast.makeText(getApplicationContext(),
                    "Application will not run without camera services", Toast.LENGTH_SHORT).show();
        }

    }
}





private void connectCamera() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);  //connects to camera

    try {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {   //android version "marshmallow" requires permissions


           if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
                    PackageManager.PERMISSION_GRANTED) {  //checks if permission is granted or not
                cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);

            } else { //if permission isn't granted. Tells user, app needs permission for camera

                if(shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    Toast.makeText(this,
                            "app requires access to camera", Toast.LENGTH_SHORT).show();
                }
                requestPermissions(new String[] { Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO
                }, REQUEST_CAMERA_PERMISSION_RESULT);
            }

        } else {
            cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, mBackgroundHandler);
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
iBEK
  • 622
  • 2
  • 8
  • 27

0 Answers0