Kazuwa
Kazuwa

Reputation: 129

checkSelfPermission always returns GRANTED

I have an Android app and I'd like to check the camera permission. However, even if I turn it off (in the app setting of the simulator or the real device), the result is always 0 (GRANTED). The simulator and real device I use is on SDK 23, Android M.

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

In the AndroidManifest.xml, I have :

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

When I log this :

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

I got this :

Build.VERSION.SdkInt : 23
permissionCheck : 0

Upvotes: 3

Views: 2624

Answers (2)

Kazuwa
Kazuwa

Reputation: 129

In fact the targetSdkVersion has to be 23 minimum in the build.gradle but the solution to this problem was 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 not ContextCompat.

Upvotes: 3

Zohaib Hassan
Zohaib Hassan

Reputation: 984

Check your "targetSdkVersion" in "build.gradle", it must be 23 or above, maybe the issue is you have set the build version to 23 but target version is less than 23. Please make sure all your sdk versions (build , target, compile) are set to 23 or above.

Upvotes: 0

Related Questions