Android: Permission Denial: Starting Intent with Revoked Permission Android.Permission.Camera

Android: Permission Denial: starting Intent with revoked permission android.permission.CAMERA

Remove this permission

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

I faced this error executing my app in android 7. After tests I noticed user permission wasn't in project A but it was in project B, that I only tested in android 5 devices. So I remove that permission in project B in order to run it on other device that targets android 7 and it finally could open.

In adittion I added the fileprovider code that Android suggests here https://developer.android.com/training/camera/photobasics.html
Hope this helps.

revoked permission android.permission.CAMERA

Here's how I solved this problem.

Contrary to what most answers say, REMOVE this from your Manifest file.

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

Why?

Starting Android M (API 23), if your app has CAMERA permission declared in the manifest, then, it needs that CAMERA permission to be GRANTED in order to access ACTION_IMAGE_CAPTURE
etc... too (which normally do not require the CAMERA permission on their own). If not, then it automatically raises a SecurityException.

╔═════════════════════════════════════════════════╦════════════════════╗
║ Usage ║ Permissions needed ║
╠═════════════════════════════════════════════════╬════════════════════╣
║ ACTION_IMAGE_CAPTURE ║ none ║
║ ACTION_VIDEO_CAPTURE ║ none ║
║ INTENT_ACTION_STILL_IMAGE_CAMERA ║ none ║
║ android.hardware.camera2 ║ CAMERA ║
║ android.hardware.camera2 + ACTION_IMAGE_CAPTURE ║ CAMERA ║
║ android.hardware.camera2 + ACTION_VIDEO_CAPTURE ║ CAMERA ║
║ ... ║ ... ║
╚═════════════════════════════════════════════════╩════════════════════╝

Above table is only for API 23+

tl;dr What to do?

Option 1- If you only use ACTION_IMAGE_CAPTURE etc...

Remove the CAMERA permission from manifest and you would be fine

Option 2- If you use other CAMERA functions:

Check for CAMERA permission at runtime and only start the intent when the permission is available

Android M Camera Intent + permission bug?

I had the same issue and find this doc from google:
https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE

"Note: if you app targets M and above and declares as using the CAMERA permission which is not granted, then atempting to use this action will result in a SecurityException."

This is really weird.
Don't make sense at all.
The app declares Camera permission using intent with action IMAGE_CAPTURE just run into SecurityException. But if your app doesn't declare Camera permission using intent with action IMAGE_CAPTURE can launch Camera app without issue.

The workaround would be check is the app has camera permission included in the manifest, if it's , request camera permission before launching intent.

Here is the way to check if the permission is included in the manifest, doesn't matter the permission is granted or not.

public boolean hasPermissionInManifest(Context context, String permissionName) {
final String packageName = context.getPackageName();
try {
final PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
final String[] declaredPermisisons = packageInfo.requestedPermissions;
if (declaredPermisisons != null && declaredPermisisons.length > 0) {
for (String p : declaredPermisisons) {
if (p.equals(permissionName)) {
return true;
}
}
}
} catch (NameNotFoundException e) {

}
return false;
}

android: FATAL EXCEPTION: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE

If you are targeting API Level 23 or more, then you should force get permissions like below(contains several permissions, you can add or remove based on your requirement). Put this in a separate class:

public static List<String> checkAndRequestPermissions(Context context) {

int camera = ContextCompat.checkSelfPermission(context, android.Manifest.permission.CAMERA);
int readStorage = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
int writeStorage = ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
int fineLoc = ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION);
int coarseLoc = ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();

if (camera != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.CAMERA);
}
if (readStorage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.READ_EXTERNAL_STORAGE);
}
if (writeStorage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (fineLoc != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.ACCESS_FINE_LOCATION);
}
if (coarseLoc != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(android.Manifest.permission.ACCESS_COARSE_LOCATION);
}

return listPermissionsNeeded;
}

And in your activity:

public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;  // Declare this integer globally

Add this method(to retrieve permissions):

private boolean permissions(List<String> listPermissionsNeeded) {

if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}

And force get permissions like this:

// In my case I've put the 'checkAndRequestPermissions' method in a separate class named 'PermissionUtils'
List<String> permissionList = PermissionUtils.checkAndRequestPermissions(this);

if (permissions(permissionList)) {

dispatchTakePictureIntent(); // call your camera instead of this method
}


Related Topics



Leave a reply



Submit