6

Note: similar question is asked over below SO links, but none of the proposed solution worked for me, hence starting this thread.

  1. MediaStore.Images.Media.insertImage is returning null when trying to save the image
  2. Why Images.Media.insertImage return null

Problem statement: I am downloading the image from a url and then trying to share the same. To achieve this I need the Uri, which I am obtaining by saving the image in MediaStore.Images.Media.

Code:

A. Obtained downloaded image from an AsyncTask class

public void processDownloadedImage(Bitmap bitmap) {
        if (bitmap == null) {
            Toast.makeText(this,"Image could not be downloaded and shared!",Toast.LENGTH_LONG).show();
        }
        else {
            downloadedImage = bitmap;
            checkPermissionShowShareSheet();
        }
    }

B. Checking permission for Marshmallow and higher

public void checkPermissionShowShareSheet() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)){
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},2909);
            }
            else {


            updateImageUri(downloadedImage);
            showShareSheet(downloadedImageUri);
        }
    }
    else {
        updateImageUri(downloadedImage);
        showShareSheet(downloadedImageUri);
    }
}

C. Obtaining image Uri

private void updateImageUri(Bitmap inImage){
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        fixMediaDir();
        String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), inImage, "SomeImage", null);
        storePathInSharedPreferences(path);
        Uri uri = Uri.parse(path);

        downloadedImageUri = uri;
    }

    void fixMediaDir() {
        File sdcard = Environment.getExternalStorageDirectory();
        if (sdcard != null) {
            File mediaDir = new File(sdcard, "DCIM/Camera");
            if (!mediaDir.exists()) {
                mediaDir.mkdirs();
            }
        }
    }

D. Manifest file uses permissions

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

Problem: value returned from below line is null

String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), inImage, "SomeImage", null);

Any ideas?

Here is portion of Stackbacktrace:

Failed to insert image java.io.FileNotFoundException: No such file or directory       
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:620)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:939)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:686)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:662)
at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:934)
at myorg.myapp.ImageDescription.updateImageUri(ImageDescription.java:165)
Community
  • 1
  • 1

1 Answers1

0

Change this

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

To

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Franklyn
  • 325
  • 2
  • 8