Android M - Camera permission denied returns PERMI

2019-08-11 14:06发布

问题:

I'm testing denial of permission to an application and I'm seeing that when asking for the state of the permission it returns granted instead of denied.

I'm checking state of permissions according to Google's Guide:

    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                Manifest.permission.CAMERA)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.CAMERA},
                    PERMISSIONS_REQUEST_TAKE_PHOTO);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }

Here is what it actually returns in the first if:

State "0" means PackageManager.PERMISSION_GRANTED

This is generating a java.lang.RuntimeException: Fail to connect to camera service error.

DEVICE IS A NEXUS 5X

Best regards.

回答1:

Quotig @CommonsWare:

You're supposed to get PERMISSION_GRANTED for any targetSdkVersion below 23, even if the user toggled off that permission group in Settings. I suggest that you uninstall the app completely, set your targetSdkVersion to 23, and try it again.

This was the solution.

(This answer will be marked correct temporarily until commenter posts an answer)