SecurityException: Permission Denial: reading (only on emulator)

TPWang picture TPWang · Sep 16, 2015 · Viewed 39.1k times · Source

I have two activities in my app, MainActivity calls ImagePicker, which has a GridView laying out all the images in the phone gallery, where I use a ContentResolver to get the cursor.

It worked fine on my phone when I tested it but crashed every time instantly on emulator.

Here's the error log:

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

I do have permission written in manifest, as shown below:

<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"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".ImagePicker" />
</application>

Answer

riggaroo picture riggaroo · Sep 16, 2015

What version of android is your emulator? And are you compiling with SDK 23? If your emulator is Marshmallow, you need to explicitly check for the permission at runtime. This is due to the new permissions model.

Check out the docs on permissions for more info: https://developer.android.com/training/permissions/index.html