Android READ_EXTERNAL_STORAGE permission not working

Zheryu picture Zheryu · Sep 2, 2015 · Viewed 31.3k times · Source

I'm trying to create a simple app that can get an image from the Gallery app and display it on an imageButton. I'm testing with API 21 on a phone running Android 5.0.1. Unfortunately, no matter what I try I keep getting a security error - even when I specify the permissions.

My code for getting retrieving the image is:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode){
        case PICK_IMAGE_REQUEST:
            if(resultCode == RESULT_OK && imageReturnedIntent != null && imageReturnedIntent.getData() != null) {
                Uri uri = imageReturnedIntent.getData();
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                    // Log.d(TAG, String.valueOf(bitmap));
                    ImageButton ib = (ImageButton) findViewById(R.id.inputImg);
                    ib.setImageBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
    }
}

The code works when I try to select an image from Dropbox, but when I select an image from gallery, I get

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/35634 from pid=25240, uid=10070 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

I made the use-permission tag for READ_EXTERNAL_STORAGE a child of manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="[REDACTED]">

    <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />

    <uses-permission tools:node="replace" android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />

    <application>
        [REDACTED]
    </application>

</manifest>

I've searched high and low on other posts, but still can't seem to figure out why I'm still getting this pesky error.

Answer

Jordi Castilla picture Jordi Castilla · Sep 2, 2015

PROBLEM
Your have the error

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/35634 from pid=25240, uid=10070 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission().

CAUSE
You are not giving correctly permissions because of android:maxSdkVersion="18", which according to documentation.

The highest API level at which this permission should be granted to your app. Setting this attribute is useful if the permission your app requires is no longer needed beginning at a certain API level.

As long as you didn't give permissions to API 21 your app is behaving ok.

Check this topic for further info.

SOLUTION
If you wanna give read permissions under API 21, you must declare them as:

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

IMPORTANT:

This only make sense when you need to give extra permissions because some old api's require permissions that new versions doesn't, so user experience get's improved because you only ask for certain permissions when needed.


So (in this case) the correct way will be:

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

ADD ON
If you want to save data you must add WRITE_EXTERNAL_STORAGE permissions.

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