我正在尝试将文件存储到外部存储设备(Android 4.2.2)。
我在manifest.xml中使用了正确的权限:
<manifest ...>
...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...
外部SD卡可用,我可以使用其他应用来证明它可以读写文件。路径/ mnt / extsd /有效,请看下图。外部SD卡是可写的,因为我可以将文件复制/粘贴到其中。
代码:
try {
String path = "/mnt/extsd/file.dat"
File f = new File(path);
f.createNewFile();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
我在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)
如果我使用String path = "/mnt/extsd/file.dat"
一切正常!那么问题出在哪里?
修改
我正在使用此解决方案获取可用的存储列表:
How can I get the list of mounted external storage of android device
并且,正如建议的那样,我发布了命令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
对命令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
对命令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
对命令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
答案 0 :(得分:1)
Response to the command adb shell ls -l /mnt
d---rwxr-x system media_rw 1970-01-01 01:00 extsd
要编写extsd
该流程需要成为群组media_rw
的一部分,但WRITE_EXTERNAL_STORAGE
会将其添加到sdcard_rw
群组。您需要的权限是WRITE_MEDIA_STORAGE
但不幸的是,其保护级别为signatureOrSystem
,因此您无法在自己的应用中使用