FileNotFoundException (permission denied) when trying to write file to sdcard in Android
Asked Answered
F

1

24

As you can notice from title, I have a problem with writing file to sdcard in Android. I've checked this question 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.

First, I check if sdcard is mounted:

Environment.getExternalStorageState();

Then, I run this 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();
    }

I have:

<manifest>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application>
...
</application>
</manifest>

in my AndroidManifest.xml.

Exact error is this:

java.io.FileNotFoundException: /storage/1510-2908/Download/secondFile.txt: open failed: EACCES (Permission denied)

I'm testing my code on emulator (emulating Nexus5 API 23). My minimum required SDK version is 19 (4.4 Kitkat).


Also, everything works fine with writing files to private folder on sdcard with same code so I'd say former code should work too:

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();
    }

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?

Fitts answered 9/2, 2016 at 6:27 Comment(5)
What is the value you got at Log.d("NEWFILE", file.getAbsolutePath()); ?Granger
#23528267Reiche
@Fitts You also need to request WRITE_EXTERNAL_STORAGE permissions at Run Time, As you are testing it "Nexus5 API 23".Sanity
i guess its because of the permission handling in android m. as in deepanker's link described, you have to add a request for the permission, befor accessing the storage in android m.Mize
@Fitts Check this and this for more detail.Sanity
S
30
  1. Everything works fine with writing files to private folder on sdcard

Beginning with Android 4.4, these permissions are not required if you're reading or writing only files that are private to your app. For more information, see saving files that are app-private.

  1. FileNotFoundException (permission denied) when trying to write file to sdcard in Android

As you are trying to write the file in emulator having API 23(Marshmallow), You need to Request WRITE_EXTERNAL_STORAGE permission at runtime also. Check this and this for more detail.

Sanity answered 9/2, 2016 at 6:52 Comment(4)
This works, but only second time I start my application. If I try to open file right there in onRequestPermissionsResult() callback when user then I receive the same error as before. Could it be that permission is not yet propagated to the system? This seems strange.Fitts
@Fitts Permission is granted to your app at the same time user allow the permission.Sanity
@Fitts Take a look at Can't write to external storage unless app is restarted after granting permission and Android 6.0 needs restart after granting user permission at runtime SO post. They have reported same problem like yours.Sanity
Thanks, it seems there is an issue with Android at the moment and the bug is reported here: code.google.com/p/android-developer-preview/issues/…Fitts

© 2022 - 2024 — McMap. All rights reserved.