简体   繁体   中英

File not found in /data/media/0, but found in /storage/emulated/0

I use the smartphone emulator in Android Studio for a Nexus 4, API 23.

I took a picture through its camera, and I can check that the file is present at this different paths through the Android Device Monitor:

/data/media/0/DCIM/Camera/IMG_20160408_143726.jpg (rw-rw-r)
/storage/emulated/0/DCIM/Camera/IMG_20160408_143726.jpg (rw-rw)

Android设备监视器

Nevertheless, in my Activity I got different behaviors when I try to read the file:

new File("/data/media/0/DCIM/Camera/IMG_20160408_143726.jpg");
// isFile at false, canRead at false, length at 0

new File("/storage/emulated/0/DCIM/Camera/IMG_20160408_143726.jpg");
// isFile at true, canRead at true, length with a real value

Why is it so?

它取决于构造器,而是使用此方法获取sdcard路径,它将为所有设备找到正确的路径

new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/IMG_20160408_143726.jpg")

You probably would like to grant your app root permission so as to let it work properly. Take a look at this example:

u0_a1@android:/ $ toolbox ls -l /storage/emulated/0/UPDATE*
-rw-rw---- root    sdcard_r  4997896 2016-09-08 20:08 UPDATE-SuperSU-v2.78.zip
u0_a1@android:/ $ toolbox ls -l /data/media/0/UPDATE*
/data/media/0/*: Permission denied
1|u0_a1@android:/ $ su
root@android:/ # toolbox ls -l /data/media/0/UPDATE*
-rw-rw-r-- media_rw media_rw  4997896 2016-09-08 20:08 UPDATE-SuperSU-v2.78.zip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM