1

I mean the followings in this question:

internal SD card: the on-board storage device whose path is obtainable from calling Environment.getExternalStorageDirectory()

external SD card: a physical microSD card that can be inserted to or removed from the device's microSD card slot


I am developing in Android 6.0 Marshmallow.

I heard that we cannot write at a custom path in external SD card in Android Lollipop, but we can write at a custom path in external SD card in Android Marshmallow.

I added the permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in the Android manifest.
I also requested the runtime permission by calling the function ActivityCompat.requestPermissions(activity, permissionArray, requestCode).

Now. I can read/write a file in the internal SD card (a path like /storage/emulated/0/...).
My problem is, that I cannot write a file in the external SD card (a path like /storage/0000-0000/...).
If I try to write a file in the external SD card, the following exception occurs:

java.io.FileNotFoundException: /storage/0000-0000/Pictures/abc.jpg: open failed: EACCES (Permission denied)
    at libcore.io.IoBridge.open(IoBridge.java:452)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
    ...

I have searched this problem in StackOverflow, but the questions/answers were all about the runtime permission in Android 6.0, which is not my case.

How can I write a file at a custom path in the external SD card in Android 6.0?


EDIT

To be clear, what I have is the path of a file to write (like a string "/storage/0000-0000/Pictures/abc.jpg"), and I can use FileOutputStream to write the content.

Naetmul
  • 14,544
  • 8
  • 57
  • 81
  • 1
    You are never allowed to write to the whole micro SD card. Only to your app specific directory like ......../Android/data/. Try getExternalFilesDirs() to find that path. It will be the second entry. ..FilesDirs not ...FilesDir. – greenapps Aug 18 '16 at 07:26
  • @greenapps Doesn't it apply only to Android 4.4 and 5.x? – Naetmul Aug 18 '16 at 07:28
  • 1
    No. It does not as far as i know. But maybe you can use the Storage Access Framework. – greenapps Aug 18 '16 at 07:28

0 Answers0