title: MediaStore.Images.Media.insertImage - java.io.FileNotFoundException - Android
body: <blockquote>
    <p>Note: similar question is asked over below SO links, but none of the
    proposed solution worked for me, hence starting this thread.</p>
  </blockquote>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/23059580/mediastore-images-media-insertimage-is-returning-null-when-trying-to-save-the-im">MediaStore.Images.Media.insertImage is returning null when trying
  to save the image</a> </li>
  <li><a href="https://stackoverflow.com/questions/12230942/why-images-media-insertimage-return-null">Why Images.Media.insertImage return null</a></li>
  </ol>
  
  <p><strong>Problem statement:</strong>
  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. </p>
  
  <p><strong>Code:</strong></p>
  
  <p>A. Obtained downloaded image from an AsyncTask class</p>
  
  <pre><code>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();
          }
      }
  </code></pre>
  
  <p>B. Checking permission for Marshmallow and higher</p>
  
  <pre><code>public void checkPermissionShowShareSheet() {
  
          if (Build.VERSION.SDK_INT &gt;= 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);
      }
  }
  </code></pre>
  
  <p>C. Obtaining image Uri</p>
  
  <pre><code>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();
              }
          }
      }
  </code></pre>
  
  <p>D. Manifest file uses permissions</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p><strong>Problem:</strong> value returned from below line is null</p>
  
  <pre><code>String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), inImage, "SomeImage", null);
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>Here is portion of Stackbacktrace:</strong></p>
  
  <pre><code>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)
  </code></pre>