Search code examples
androidandroid-storage

checkSelfPermission returning false even if Permission is granted


So I've been sitting here for 4 hours now, scratching my head as to why are my 2 devices misbehaving. The problem is, checkSelfPermission is returning false, even if the permission is granted! I'm 100% sure that this shouldn't happen, as it worked as expected 5-6 days ago. Here's the code I'm using to check if the permission is granted or not.

public boolean isStoragePermissionGranted() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                Log.v("BaseActivity", "Permission is granted");
                return true;
            } else {
                Log.v("BaseActivity", "Storage Permission is revoked");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
                return false;
            }
        } else { //permission is automatically granted on sdk<23 upon installation
            Log.v("BaseActivity", "Permission is granted");
            return true;
        }
    }

Now, whenever I open the activity (BaseActivity) which has this code for the first time, it works as expected and requests user for the Storage permission. But, in the fragment which is in this activity, where I want to access the files, whenever I call this method, it has started returning false. I've made this method public and it used to return true/false depending on the situation. Here's how I check if permission is granted in Fragment.

if (!((BaseActivity) getActivity()).isStoragePermissionGranted()) {
                        Toast.makeText(getActivity(), getString(R.string.storagePermissionError), Toast.LENGTH_LONG).show();
                        break;
                    }

I'm on compileSdkVersion and targetSdkVersion 28, so scoped external storage permissions shouldn't be the problem. What am I doing wrong here? Why does isStoragerPermissionGranted() method always returning false even if I 100% have given STORAGE permission to my app?


Solution

  • So I found what was wrong with the app. The problem was Vungle ads SDK, which set

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

    And according to https://developer.android.com/guide/topics/manifest/uses-permission-element#maxSdk the system wouldn't grant WRITE_EXTERNAL_STORAGE permission even if it was directly set in the Settings itself.

    So this SDK set this tag, and it was pretty hard to find because it was inside Merged Manifest where I was able to luckily spot this thing.

    To overcome this problem without removing the SDK from your app, do the following thing: Add tools:remove="android:maxSdkVersion"

    So it becomes like this:

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

    to the tag which has maxSdkVersion set in your own Manifest.xml.