title: Can't save files on External Storage, even with user permissions [Android]
body: <p>I'm developing an app for images manipulation on Android, but I'm stuck in writing the code for image saving.</p>
  
  <p>Here's the method I use:</p>
  
  <pre><code>private void saveImageToExternalStorage(Bitmap finalBitmap) {
      String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
      File myDir = new File(root + "/saved_images");
      myDir.mkdirs();
      Random generator = new Random();
      int n = 10000;
      n = generator.nextInt(n);
      String fname = "Image-" + n + ".jpg";
      File file = new File(myDir, fname);
      if (file.exists())
          file.delete();
      try {
          FileOutputStream out = new FileOutputStream(file);
          finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
          out.flush();
          out.close();
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  
  
      // Tell the media scanner about the new file so that it is
      // immediately available to the user.
      MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
              new MediaScannerConnection.OnScanCompletedListener() {
                  public void onScanCompleted(String path, Uri uri) {
                      Log.i("ExternalStorage", "Scanned " + path + ":");
                      Log.i("ExternalStorage", "-&gt; uri=" + uri);
                  }
              });
  
  }
  </code></pre>
  
  <p>Every time I execute this, FileOutputStream gives a FileNotFoundException.</p>
  
  <p>My manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.gabrielerestuccia.testimmagini"&gt;
  
  &lt;application
      ...
  &lt;/application&gt;
  
  &lt;uses-feature android:name="android.hardware.camera" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I'm developing this on Android Studio 1.5.1 on Linux, using the built-in virtual Nexus 5 with Android 6.0.</p>
  
  <p>Thanks for the help.</p>