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;
}

Leave a Comment