-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
  • 38,249
  • 74
  • 184
  • 323

2 Answers2

1

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

Rajesh
  • 2,558
  • 17
  • 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
0

Starting with Android 6.0, permissions are requested at runtime, and not before the installation.

Now, where you need to perform something that requires a permission (in your case, writing a file on the external storage), you have to do this:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission. WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);

        // MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

Then, override `` to see if your user has granted the permission or not.

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] ==  PackageManager.PERMISSION_GRANTED) {
                // permission was granted, yay!
            } else {
                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

You can get more information about the new permission model here.

Gaëtan
  • 11,042
  • 5
  • 32
  • 45
  • In your code, when showing explanation, why `Show an explanation to the user *asynchronously* `? If I just popup a dialog to explain to user why the permission is needed, why I need to pop up dialog asynchronously? It is not a long running thing. I checked also the offical doc, it says the same, I am wondering do I have to follow that? – Leem.fin Jan 07 '17 at 12:38