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.