简体   繁体   中英

Android READ_EXTERNAL_STORAGE permission not working

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.

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 r equires 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" />

Simply replace with the below one and try,

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

在某些设备上进行调试时,您需要通过转到设置 --> 应用程序 --> MY_APPLICATION --> 权限,然后授予所需权限来明确授予权限。

In addition to adding the below in AndroidManifest.xml

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

we also need to request permission in the activity for the app to actually have access to external storage. https://developer.android.com/training/permissions/requesting.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM