android ContextCompat.checkSelfPermission always returns PERMISSION_GRANTED

Viewed 1962

In my android application (minSdkVersion 15), i have to create directory and write files into it. In my AppManifest i am using :

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

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

Before creating directory or file, i am checking if permission is allowed or deny, as

   boolean isAllow = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

But it is always returning true or PERMISSION_GRANTED, even when someone set deny permission from Settings -> Apps -> Permissions

Deny Permission

Why checkSelfPermission is always returning PERMISSION_GRANTED ? Is there any way to check if permission is denied ?

To invoke permission i have used :

  ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

But its not showing permission Dialogue??

3 Answers

For those who develop with Xamarin make sure that you explicitly set the targetSDK version >= 23 in your manifest.

Don't use automatic for your targetSDK!!!

if your targetSdkVersion is minimum 23 then your problem can be solved by code as below:

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