Nicholas Pesa Nicholas Pesa - 5 months ago 80
Android Question

cannot request READ_EXTERNAL_STORAGE permission

I am trying to read image paths from the storage and i keep getting the SecurityException here:

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


I am compiling against api 23 and have the permissions clearly stated in my Manifest as a direct child of my Manifest. I understand as well that the new runtime permissions state you have to ask for permission during runtime when you want to access the storage. When i write this line that would begin the check for the permission i get a cannot resolve issue in the IDE:

ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)


cannot resolve symbol Manifest.permission.READ_EXTERNAL_STORAGE. The only Manifest.permission that i can clearly state in my code is Manifest.permission.C2D_MESSAGE without getting a cannot resolve error. Is there something else i need to compile to have access to the other permissions in code?

Just to be clear this is the block of code that is giving me the SecurityException:

public List<String> getCameraImagesFull(Context context) {
Cursor cursor = null;
String[] star = {"*"};
ArrayList<String> result = new ArrayList<>();
cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, star, null, null, MediaStore.MediaColumns.DATE_ADDED + " DESC");
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
result.add(path);
} while (cursor.moveToNext());
}
cursor.close();
}
return result;
}


this is the line that throws the exception:

cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, star, null, null, MediaStore.MediaColumns.DATE_ADDED + " DESC");


And here are all the permissions stated in my manifest:

<permission
android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />


i am just wondering how i go about requesting that permission so i can get the image paths. This all works when i compile against any Lollipop build, just with the new runtime permissions on Marshmallow im getting the SecurityExeption. Any and all help is appreciated, thanks in advance

Answer

cannot resolve symbol Manifest.permission.READ_EXTERNAL_STORAGE

You need to add an import statement for android.Manifest, the same way you do with any other Java class.

The only Manifest.permission that i can clearly state in my code is Manifest.permission.C2D_MESSAGE without getting a cannot resolve error

There is no such value (C2D_MESSAGE) in android.Manifest.permission. I don't even know where there is a Manifest class, with a permission inner class, with a field named C2D_MESSAGE.