Android permission READ_EXTERNAL_STORAGE not working

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

My code for getting the image:

@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 the 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 a usage permission tag for the READ_EXTERNAL_STORAGE child of the 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 searched high and low on other posts, but still can't figure out why I am still getting this nasty error.

+11
java android android-5.0-lollipop android-permissions android-gallery
Sep 02 '15 at 10:38
source share
4 answers

PROBLEM
You have a mistake

java.lang.SecurityException: permission denied: 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 do not give the correct permissions because of android:maxSdkVersion="18" , which is according to the documentation.

The highest level of API at which this permission should be granted to your application. Setting this attribute is useful if you need the permission required by your application, starting from a specific API level.

Until you grant permissions for API 21 , your application behaves normally.

Open this section for more information.

Decision
If you want to grant 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 makes sense when you need to grant additional permissions, because some old api require permissions for new versions, so the user interface improves because you only ask for specific permissions if necessary.




So (in this case) the correct path would be:

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



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

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+13
Sep 02 '15 at 10:56
source share
— -

Just replace the bottom and try,

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+2
02 Sep '15 at 10:44
source share

When debugging on some devices, you must explicitly grant permission by going to your settings -> apps -> MY_APPLICATION -> Permissions, and then grant the required permissions.

+1
Nov 30 '16 at 13:23
source share

In addition to adding below in AndroidManifest.xml

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

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

0
Jun 08 '17 at 4:34 on
source share



All Articles