0

There are many questions about this topic, but I cannot find any answers for my corrective example.

I'm using Samsung galaxy S5 run android 4.4, which is limited for storage

The official document said:

The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled.

My application need to write files to Sdcard (Absolute path is /storage/extSdCard), so I write my app data to my app directory: /storage/extSdCard/Android/com.myapp.example/files but got permission denied exception. So I suspect the above statement:

except in their package-specific directories as allowed by synthesized permissions

I think I cannot write to root directory /storage/extSdCard but still able to write my app data to my app package directory. Did I misunderstand something here?

p/s: I still able to write my app data to built-in storage: /storage/emulated/0/Android/data/com.myapp.example/files. I don't want to use getExternalFileDirs() because it always return built-in, not my sdcard directory.

LuongPham
  • 81
  • 10

2 Answers2

3

If getExternalFilesDir(null) is returning somewhere different to /storage/extSdCard/Android/com.myapp.example/files, then I would think that is why it is giving you access permission errors. The only place on the SD card you can write to without permissions is the directory returned by getExternalFilesDir()

Since you say the directory returned by getExternalFilesDir(null) is not acceptable, I would suggest adding the WRITE_EXTERNAL_STORAGE permission to your manifest.

TomW
  • 310
  • 1
  • 7
  • 1
    In my app, getExternalFilesDir(null) return "/storage/emulated/0/Android/data/com.myapp.example/files" (which belong to Phone built-in storage), of course I'm freely to write file on this. But the same path on Sdcard ("/storage/extSdCard/Android/data/com.myapp.example/files"), I cannot do anything although they said: "Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions" --> means I still able to write my app package directory on my Sdcard. – LuongPham Oct 07 '15 at 16:23
  • 1
    I've already added the WRITE_EXTERNAL_STORAGE permission to my manifest. – LuongPham Oct 07 '15 at 16:25
  • Ah so the getExternalFilesDir is referring to the built in storage, and you have another SD card that you're attempting to access. (Android has terrible terminology for these cases) Are you sure that the SD card is mounted when you are testing it? If the card is showing up in your computer, it may not be accessible to Android. To check you could try adb shell, and try to navigate to it, or download a program such as ES File Explorer and attempt to navigate there with it. – TomW Oct 08 '15 at 03:36
  • @TomWijgers , I have the same issue as LuongPham , the removable storage location is accessible and canWrite property return true , have given manifest permission. When I try to create directory with mkdir or mkdirs , nothing happens. So I tried creating files manually and on writing an image file , got a permission denied error. Same code works for internal and emulated external. – Dinesh Balu Dec 10 '15 at 07:35
1

Your app specific directory should be /storage/extSdCard/Android/data/com.myapp.example/files and not /storage/extSdCard/Android/com.myapp.example/files

somesh
  • 2,509
  • 3
  • 16
  • 24
  • 1
    Sorry I post wrong path, the correct path of my app is `/storage/extSdCard/Android/data/com.myapp.example/files` (belong to `Sdcard`). But I cannot `mkdirs()` on this path, if I create this directory by system file manager application, then `createNewFile()` on this, example: `/storage/extSdCard/Android/data/com.myapp.example/files/myfile.txt` I got `EACCES permission denied`. In my example, I could only allow to access on `/storage/emulated/0/Android/data/com.myapp.example/files/myfile.txt` (Belong to `phone built-in storage`). – LuongPham Oct 07 '15 at 16:19
  • The only thing I found is here https://developer.android.com/guide/topics/providers/document-provider.html#client – Đốc.tc May 24 '19 at 04:12