miXo
miXo

Reputation: 313

Android write access to SD card

I am trying to write to External secondary storage (sd card), but it looks like I do not have the permission.

According to Android:

"Secondary external storage must not be writable by apps, except in package-specific directories as allowed by synthesized permissions."

So this wont work:

try {
    String sdCard = System.getenv("SECONDARY_STORAGE");
    FileWriter writer = new FileWriter(new File(sdCard, "test.txt"));
    writer.write("a test");
    writer.flush();
    writer.close();
} catch (IOException e) {
    Log.d(e.toString());
    //java.io.FileNotFoundException: /storage/extSdCard/test.txt: open failed: EACCES (Permission denied) 
}

I have the permission in AndroidManifest.xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

and Environment.getExternalStorageDirectory() wont work since I am trying to write to removable sd card not the phone memory.

So, if apps supposed to be prohibited to write to secondary storage, how can apps like File Manager write to sd cards?

Upvotes: 2

Views: 1533

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007584

So this wont work:

Among other reasons, I am not aware of a requirement for devices to have a SECONDARY_STORAGE environment variable.

I have the permission in AndroidManifest.xml

That is for external storage, not removable storage.

how can apps like File Manager write to sd cards?

They are relying on users helping them via the Storage Access Framework.

Upvotes: 5

Related Questions