Java – checkSelfPermission always returns GRANTED

checkSelfPermission always returns GRANTED… here is a solution to the problem.

checkSelfPermission always returns GRANTED

I

have an Android app and I want to check the camera permissions. HOWEVER, EVEN IF I CLOSE IT (IN THE EMULATOR OR IN THE APP SETTINGS OF A REAL DEVICE), THE RESULT IS ALWAYS 0 (GRANTED).
The emulator and real device I use is SDK 23, Android M.

int permissionCheck = ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA);

In AndroidManifest.xml, I have:

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

When I record this:

System.out.println("Build.VERSION.SdkInt : " + VERSION. SDK_INT);
System.out.println("permissionCheck : " + permissionCheck);

I see:

Build.VERSION.SdkInt : 23
permissionCheck : 0

Solution

In fact, targetSdkVersion must be at least 23 in build.gradle, but the solution to this problem is to use:

int permissionCheck = PermissionChecker.checkSelfPermission(getReactApplicationContext(), Manifest.permission.CAMERA);

Instead of:

int permissionCheck = ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA);

PermissionChecker returns the correct answer but does not return ContextCompat.

Related Problems and Solutions