title: FileNotFoundException (permission denied) when trying to write file to sdcard in Android
body: <p>As you can notice from title, I have a problem with writing file to sdcard in Android. I've checked <a href="https://stackoverflow.com/questions/4823992/java-io-filenotfoundexception-permission-denied-when-trying-to-write-to-the-an">this question</a> but it didn't help me. I want to write file that will be in public space on sdcard so that any other app could read it.</p>
  
  <p>First, I check if sdcard is mounted:</p>
  
  <pre><code>Environment.getExternalStorageState();
  </code></pre>
  
  <p>Then, I run this code:</p>
  
  <pre><code>File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
      baseDir.mkdirs();
      File file = new File(baseDir, "file.txt");
      try {
          FileOutputStream out = new FileOutputStream(file);
          out.flush();
          out.close();
          Log.d("NEWFILE", file.getAbsolutePath());
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>I have:</p>
  
  <pre><code>&lt;manifest&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
  &lt;application&gt;
  ...
  &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>in my AndroidManifest.xml.</p>
  
  <p>Exact error is this:</p>
  
  <pre><code>java.io.FileNotFoundException: /storage/1510-2908/Download/secondFile.txt: open failed: EACCES (Permission denied)
  </code></pre>
  
  <p>I'm testing my code on emulator (emulating Nexus5 API 23). My minimum required SDK version is 19 (4.4 Kitkat).</p>
  
  <hr>
  
  <p>Also, everything works fine with writing files to private folder on sdcard with same code so I'd say former code should work too:</p>
  
  <pre><code>File newFile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "esrxdtcfvzguhbjnk.txt");
      newFile.getParentFile().mkdirs();
      try {
          FileOutputStream out = new FileOutputStream(newFile);
          out.flush();
          out.close();
          Log.d("NEWFILE", newFile.getAbsolutePath());
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Does anyone have any clue what could be the problem? Could it be that somehow KitKat 4.4 just doesn't allow writing to public space in sdcard anymore or?</p>