BP233
BP233

Reputation: 85

Android - SecurityException when querying a ContentResolver with Media.EXTERNAL_CONTENT_URI

I have add in my AndroidManifest.xml as below, but I get a SecurityException everytime I click the Button.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.root.musictest" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

The Button Listener:

private ArrayList<String> names = new ArrayList<String>();
public void findItems(View view) {
Cursor cursor = getContentResolver().query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null, null, null, null);
while(cursor.moveToNext()) {
    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
    names.add(name);
}
list.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.id.text1,
            android.R.layout.simple_list_item_1,
            names));
}

The log is:

Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2506, uid=10083 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
    at android.os.Parcel.readException(Parcel.java:1465)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:420)
    at android.content.ContentResolver.query(ContentResolver.java:461)
    at android.content.ContentResolver.query(ContentResolver.java:404)
......

How can I solve it? Any input will be help. Thanks

Upvotes: 2

Views: 2024

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007474

Replace:

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

with:

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

The big thing is the case — you are tripping over an Android Studio bug that put your permissions in with all-caps. You also don't need READ_EXTERNAL_STORAGE if you hold WRITE_EXTERNAL_STORAGE, AFAIK.

Upvotes: 2

Related Questions