1

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

JaySparkexel
  • 165
  • 4
  • 16
  • Possible duplicate of [READ\_EXTERNAL\_STORAGE permission for Android](https://stackoverflow.com/questions/32431723/read-external-storage-permission-for-android) – Tomin B Azhakathu Dec 17 '17 at 13:47
  • After your suggestion I removed Write permission from manifest file. But I still got the same error. @tomin-b – JaySparkexel Dec 17 '17 at 13:59
  • see this question https://stackoverflow.com/questions/21275898/securityexception-with-granturipermission-when-sharing-a-file-with-fileprovider – Mohamed Mohaideen AH Dec 17 '17 at 15:28

1 Answers1

3

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.

Iman Marashi
  • 5,593
  • 38
  • 51
Relish Wang
  • 385
  • 1
  • 8
  • Thanks a lot. That was my silly mistake. I have changed it to WRITE but than pop up which ask for permission does not show at all. and toast showing message "Until you grant the permission, we cannot save the photo" shows up. which is invoked if permission not granted in my code. @relish-wang – JaySparkexel Dec 17 '17 at 16:23
  • It looks like it was problem specific to emulator. After applying your suggestion and Testing it on actual Android device it worked. Thanks @relish-wang. – JaySparkexel Dec 17 '17 at 18:42