-1

My manifest.xml is like this:

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

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

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

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

</manifest>

As you can see I have the :

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

But when I press a button in my app which triggers the following code:

 MediaStore.Images.Media.insertImage(
    getContentResolver(), myView.getDrawingCache(),
    UUID.randomUUID().toString()+".png", "my pic");

I got following exception:

Failed to insert image
java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2574, uid=11336 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
                                                                        at android.os.Parcel.readException(Parcel.java:1627)

Why? I have the permission defined in Androidmanifest file.

Leem.fin
  • 40,781
  • 83
  • 202
  • 354

1 Answers1

1

downgrade from targetSdkVersion 24 to targetSdkVersion 22 or add permission programmatic. see official doc

Rajesh
  • 2,618
  • 19
  • 25
  • I got the same error even though changed targetSdkVersion to lower than 23. (I use targetSdkVersion 17) – Leem.fin Jan 07 '17 at 11:21