我似乎无法弹出对话框以获取相机请求权限。我没有看到我犯了什么错误。我认为我已经很好地遵循了Android开发人员文档的权限,但我仍然没有看到有关权限的对话框。代码有什么问题,我该怎么做才能修复它?
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();
}
}