What I am trying to do:

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 Snippet:

A. Obtained downloaded image

//this is returned from onPostExecute method of an asyntask
    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 Stack backtrace:

    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)

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/9e060cc8-0470-442b-84ec-e7e218e72ba3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to