sdcard文件目录分析(含修正)

关于文件软链接,这个问题曾经困扰过我,后来经过正则排除了软链接的影响.下面是以前找到的资料,坐下备份.以后遇到还能解释.

1

在Android 4.2版本之前,通过Environment.getExternalStorageDirectory()获取的sdcard默认目录是正常的,可进行读写,一般的结果是“/mnt/sdcard”,但是在4.2版本之后,获取的sdcard目录是“/storage/emulated/0”,使用File.exist()方法返回true表示文件/目录是存在的,但是通过adb向该返回的目录中写数据(上传文件=)是不成功的,返回的错误是"No such file or directory",个人判断是返回的路径“/storage/emulated/0”其实是个类似快捷方式的文件,通过特定的解析方式可以认为是目录,但对于adb来说,该路径就是个文件,adb没有做过多的属性=判断。上述判断也是有依据的,通过adb的shell命令获取指定目录下的文件/文件夹列表, 该路径显示的是文件,且有大小。

之前就有猜测,关于目录结构中会存在不同程度的虚拟链接文件,google了下确实如此,以下的3处虚拟符号链接(类似windows下的快捷方式)是:

"/storage/emulated/0 and /storage/emulated/0/0 (new and "backup" locations, respectively)
/storage/emulated/legacy and /storage/emulated/legacy/0 (new and "backup" locations, respectively)
/storage/sdcard0 and /storage/sdcard0/0 (new and "backup" locations, respectively)" (文章链接是http://androidforums.com/verizon-galaxy-nexus-all-things-root/649940-4-2-sdcard-sdcard-0-observation.html)

所以在处理sdcard目录时,尽量自己先判断处理后提供同一的接口

2

关于目录/storage/emulated/legacy/目录的问题
因为google在4.2中考虑多用户的问题,对每个用户(user)来说,看各自的文件夹可以,但对于数据文件夹的处理就稍微麻烦了,所以调整了数据的挂载结构,如使用fuse技术/dev/fuse 会被挂载到/storage/emulated/0 目录,为了兼容以前,同时挂载到 /storage/emulated/legacy (故名思议,传统的),还建立三个软连接 /storage/sdcard0 ,/sdcard,/mnt/sdcard ,都指向 /storage/emulated/legacy/,(链接参见:http://bbs.gfan.com/android-5382920-1-1.html),但是就可能造成获取文件目录中的文件(如image)时,会出现相同的图片(本来上传1张,但出现了2张==)
将上面的类的判断修改如下

另外,针对image的上传后通过provider获取image图片时同一张图片给了2份,地址分别是"/storage/emulated/legacy/dcim/camera/1.jpg"和"/storage/sdcard0/DCIM/Camera/1.jpg",其中legacy的属于兼容的,但获取能获取出来,所以获取时要进行过滤处理

3

安卓系统的问题,android 4.2使用fuse技术/dev/fuse 会被挂载到/storage/emulated/0 目录,为了兼容以前的版本,同时挂载到 /storage/emulated/legacy (故名思议,传统的),还建立三个软连接 /storage/sdcard0 ,/sdcard,/mnt/sdcard ,都指向 /storage/emulated/legacy。也就是说文件夹0和legacy指向的地址是一致的,都是存储盘里的,楼主想放到哪个文件夹都可以。

4

In short: It has to do with the multi-user functionality introduced with Jelly Bean:

  • /storage/emulated/0/: to my knowledge, this refers to the "emulated MMC" ("owner part"). Usually this is the internal one. The "0" stands for the user here, "0" is the first user aka device-owner. If you create additional users, this number will increment for each.

  • /storage/emulated/legacy/ as before, but pointing to the part of the currently working user (for the owner, this would be a symlink to /storage/emulated/0/). So this path should bring every user to his "part".

  • /sdcard/: According to a comment by Shywim, this is a symlink to...

    • /mnt/sdcard (Android < 4.0)
    • /storage/sdcard0 (Android 4.0+)
  • /storage/sdcard0/: As there's no legacy pendant here (see comments below), the "0" in this case rather identifies the device (card) itself. One could, eventually, connect a card reader with another SDCard via OTG, which then would become /storage/sdcard1 (no proof for that, just a guess -- but I'd say a good one)

Though one might get to the conclusion there should be a /storage/sdcard/legacy as well, there isn't (see comments) -- which completely makes sense with my assumption of the numbers here are not related to the user, but rather to possible multiple cards: "0" would always be the one in the card-slot of the device, so no need for a "legacy symlink" here.