JaySparkexel
JaySparkexel

Reputation: 165

Failed to Insert Image. Permission Denied. Despite providing permission

I'm trying to save Bitmap Image in Gallery of Android Device using MediaStore. Here is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackagename.myappname">
<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:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mypackagename.myappname.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"></meta-data>
    </provider>
    <activity android:name=".MainActivity">
        <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 in the manifest file I have provided read and write external storage permission. I have also provided URI permission. I implemented Runtime Permission. Here's my MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    testButton = (Button) findViewById(R.id.button2);

    testButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_EXTERNAL_STORAGE);
                }
            } else {
                startSave();
            }
        }
    });

}

public void startSave() {
    imageView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
    String savedImageURL = MediaStore.Images.Media.insertImage(
            getContentResolver(),
            bitmap,
            "EditedImage",
            "MyApp"
    );
}

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSION_REQUEST_EXTERNAL_STORAGE){
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
            startSave();
        } else {
            Toast.makeText(this, "Until you grant the permission, we cannot save the photo", Toast.LENGTH_SHORT).show();
        }
    }
}

I have also checked read permission using:

private boolean checkWriteExternalPermission(){
    String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
    int res = getContext().checkCallingOrSelfPermission(permission);
    return (res == PackageManager.PERMISSION_GRANTED);            
}

It also returned false that means I did not get write permission. I have assigned permission at run time and in manifest file. But still I got this error:

E/MediaStore: Failed to insert image
          java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=6946, uid=10082 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

I have provided both the permission in manifest file and storage read permission at run time. So, why I keep getting this error? Does anyone have any idea? I'm using android emulator here for app testing. How should I implement permission to save Bitmap to gallery using mediaStore functionality of android? I also added screenshot of application permission from setting>app>myapp>permission. enter image description here

Upvotes: 1

Views: 3252

Answers (1)

Relish Wang
Relish Wang

Reputation: 385

In your testButton's OnClick:

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.**WRITE**_EXTERNAL_STORAGE}, PERMISSION_REQUEST_EXTERNAL_STORAGE);

Change READ to WRITE. Inserting image is a WRITING operation. You need to request the WRITE permission.

Upvotes: 3

Related Questions