6

I'm trying to store a file into the external storage (Android 4.2.2).

I'm using the correct permission in the manifest.xml:

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

The external sd card is available, I can prove it using other app to read and write files. The path /mnt/extsd/ is valid, look at the picture below. The external sd card is writable because I can copy/paste files into it.

enter image description here

The code:

try {
    String path = "/mnt/extsd/file.dat"
    File f = new File(path);
    f.createNewFile();
    return true;
} catch (Exception e) {
    e.printStackTrace();
    return false;
}

I get the error on f.createNewFile();:

W/System.err(2172): java.io.IOException: open failed: EACCES (Permission denied)
W/System.err(2172):     at java.io.File.createNewFile(File.java:948)
W/System.err(2172):     at solarapp.activities.settings.application.ActivityBackup$BackupTask.doInBackground(ActivityBackup.java:77)
W/System.err(2172):     at solarapp.activities.settings.application.ActivityBackup$BackupTask.doInBackground(ActivityBackup.java:1)
W/System.err(2172):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err(2172):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
W/System.err(2172):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err(2172):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
W/System.err(2172):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
W/System.err(2172):     at java.lang.Thread.run(Thread.java:856)
W/System.err(2172): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
W/System.err(2172):     at libcore.io.Posix.open(Native Method)
W/System.err(2172):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
W/System.err(2172):     at java.io.File.createNewFile(File.java:941)

If I use String path = "/mnt/extsd/file.dat" everything works fine! So, where is the problem?

EDIT

I'm using this solution to get the available storage list:

How can I get the list of mounted external storage of android device

And, as suggested, I post the result of the command adb shell cat /proc/mounts:

rootfs / rootfs rw 0 0
tmpfs /dev tmpfs rw,nosuid,relatime,mode=755 0 0
devpts /dev/pts devpts rw,relatime,mode=600 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
none /acct cgroup rw,relatime,cpuacct 0 0
tmpfs /mnt/secure tmpfs rw,relatime,mode=700 0 0
tmpfs /mnt/asec tmpfs rw,relatime,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,relatime,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,relatime,cpu 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
/dev/block/by-name/system /system ext4 ro,relatime,data=ordered 0 0
/dev/block/by-name/cache /cache ext4 rw,nosuid,nodev,noatime,nomblk_io_submit,data=ordered 0 0
/dev/block/by-name/data /data ext4 rw,nosuid,nodev,noatime,nomblk_io_submit,noauto_da_alloc,data=ordered 0 0

/dev/block/vold/93:72 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0

/dev/block/vold/93:72 /mnt/secure/asec vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0

tmpfs /mnt/sdcard/.android_secure tmpfs ro,relatime,size=0k,mode=000 0 0

/dev/block/vold/179:1 /mnt/extsd vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1023,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0

Response to the command adb shell cat /proc/partitions

 major  minor    #blocks name
  93        0      32768 nanda
  93        8      16384 nandb
  93       16      16384 nandc
  93       24     786432 nandd
  93       32    1048576 nande
  93       40      16384 nandf
  93       48      32768 nandg
  93       56     524288 nandh
  93       64     131072 nandi
  93       72    4409344 nandj
 179        0    3849216 mmcblk0
 179        1    3845120 mmcblk0p1

Response to the command adb shell cat /proc/devices and adb shell ls -l /mnt

Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc
 13 input
 29 fb
 81 video4linux
100 telephony
108 ppp
116 alsa
128 ptm
136 pts
150 cedar_dev
166 ttyACM
180 usb
188 ttyUSB
189 usb_device
216 rfcomm
247 lcd
248 capture
249 disp
250 ttyGS
251 ttyS
252 sunxi_pwm
253 bsg
254 rtc

Block devices:
259 blkext
  7 loop
  8 sd
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
 93 nand
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
179 mmc
254 device-mapper
/system/bin/sh: cat: and: No such file or directory
/system/bin/sh: cat: adb: No such file or directory
/system/bin/sh: cat: shell: No such file or directory
/system/bin/sh: cat: ls: No such file or directory
/system/bin/sh: cat: -l: No such file or directory
/system/bin/sh: cat: /mnt: Is a directory

Response to the command adb shell ls -l /mnt

drwxr-xr-x root     system            2014-04-22 11:56 asec
d---rwxr-x system   media_rw          1970-01-01 01:00 extsd
drwxrwx--- media_rw media_rw          2014-04-22 11:56 media
drwxr-xr-x root     system            2014-04-22 11:56 obb
d---rwxr-x system   sdcard_rw          2014-04-22 12:26 sdcard
drwx------ root     root              2014-04-22 11:56 secure
drwx------ shell    shell             2014-04-22 11:56 shell
d--------- system   system            2014-04-22 11:56 usbhost1
Community
  • 1
  • 1
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • uhm `d---rwxr-x system media_rw 1970-01-01 01:00 extsd`, so extsd belongs to system and can be access to whom belongs to media_rw group. I checked the permssion file on my phone. WRITE_EXTERNAL_STORAGE add the process of your app to the `sdcard_rw` group (), so you are able to write in d---rwxr-x system sdcard_rw 2014-04-22 12:26 sdcard – Blackbelt Apr 22 '14 at 12:28
  • I'm not sure I understand your conclusion... – Seraphim's Apr 22 '14 at 12:35
  • 1
    you can not write it because your app is not inside the media_rw group. You can try to add the android.permission.WRITE_MEDIA_STORAGE permssion – Blackbelt Apr 22 '14 at 12:41
  • Is there any solution? Why the folder browser can copy/paste to extsd? Can I add my app to media_rw group? – Seraphim's Apr 22 '14 at 12:48
  • I checked `adb shell cat /system/etc/permissions/platform.xml` and I found that `android.permission.WRITE_MEDIA_STORAGE` add the process to media_rw group – Blackbelt Apr 22 '14 at 12:53
  • Eclipse marks it as an error and says: "Permission is only granted to system apps". So I change LINT settings, and now I can compile. I discovered an answer http://stackoverflow.com/questions/12853752/eaccess-permission-denied-in-android and I think I'll never be able to write to the external sd card using the device I'm using for test. – Seraphim's Apr 22 '14 at 13:25
  • 1
    as I said in the beginning, it was not worth to investigate. – Blackbelt Apr 22 '14 at 13:31
  • @blackbelt I'm pretty sure everything I'm trying to do will be unuseful when porting to kitkat... anyway thanks. If you respond to the question I'll vote and accept the answer. – Seraphim's Apr 22 '14 at 15:13

1 Answers1

1
Response to the command adb shell ls -l /mnt


d---rwxr-x system   media_rw          1970-01-01 01:00 extsd

to write extsd the process need to be part of group media_rw but WRITE_EXTERNAL_STORAGE add it to the sdcard_rw group. The permission you need is WRITE_MEDIA_STORAGE but, unfortunately, its protection level is signatureOrSystem , so you can not use in your app

Blackbelt
  • 156,034
  • 29
  • 297
  • 305