java.io.IOException: open failed: EACCES (Permission denied) when writing file in API level 23
<p>I'm trying to export my DB to a CSV file. I have applied the appropriate permission to the <code>AndroidManifest.xml</code>. I'm just wondering why I can't write/create the file in my (actual device) Nexus 5X API level 23 where the same code works in my emulator Nexus 7 API level 22. Is the permission not enough?</p>

<p>Here's the code:</p>

<pre><code>private void writeCsv() {
    File fileDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),
             File.separator + "Example");
    if (!fileDir.exists()) {
        try {
                fileDir.mkdirs();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),
            File.separator
            + "Example"
            + File.separator
            + (new Date()) + ".csv");
    if (!file.exists()) {
        try {
            file.createNewFile(); // THIS IS WHERE THE EXCEPTION POINTS OUT
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    if (file.exists()) {
        try {
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bfWriter = new BufferedWriter(fileWriter);

            bfWriter.write("Id,Text,Timestamp\n");
            mDatabase.getWritableDatabase();
            List&lt;Text&gt; texts = mDatabase.getAllTexts();
            for (Text text : texts) {
                bfWriter.write(text.getId() + "," + text.getText()
                        + "," + text.getTimestamp() + "\n");
            }

            bfWriter.close();
            mDatabase.close();

            Toast.makeText(getActivity(),
                    "Texts exported to " + file.getAbsolutePath(),
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
</code></pre>

<p>My <code>AndroidManifest</code>:</p>

<pre><code>&lt;application
...
&lt;/application&gt;

&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
</code></pre>

<p>Here's the log:</p>

<pre><code>01-21 22:55:22.589 25530-25530/com.example.android W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at java.io.File.createNewFile(File.java:939)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.example.android.settings.SettingsPage.writeCsv(SettingsPage.java:166)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.example.android.settings.SettingsPage.onClick(SettingsPage.java:89)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.view.View.performClick(View.java:5204)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.view.View$PerformClick.run(View.java:21153)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.os.Looper.loop(Looper.java:148)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-21 22:55:22.599 25530-25530/com.example.android W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
01-21 22:55:22.602 25530-25530/com.example.android W/System.err:     at libcore.io.Posix.open(Native Method)
01-21 22:55:22.602 25530-25530/com.example.android W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-21 22:55:22.603 25530-25530/com.example.android W/System.err:     at java.io.File.createNewFile(File.java:932)
01-21 22:55:22.603 25530-25530/com.example.android W/System.err:    ... 11 more
</code></pre>