我有一台中兴的Android手机,型号是 ZTE U930HD,手机没有插入外置SD卡(也就是Micro SD Card,原名Trans-flash Card(TF卡),2004年正式改名为Micro SD Card),可是机身自带了一个内置存储卡(也就是eMMC存储,大小为2G)。
我把这个手机用数据线插到电脑上,也会看到盘符,经过安装「R.E 管理器」等文件管理应用,也能够管理文件,而且能看到该存储的挂载目录是:/mnt/sdcard2
可是,
我打印 Environment.getExternalStorageState(),却返回 ”removed“;
这是怎么回事?明明手机自己带着内置SD卡,却为什么提示这么一个信息?
我又试着去打印了Environment.getExternalStorageDirectory(),返回:“/mnt/sdcard”
看起来能够解释了,在我这个中兴手机上,调用Environment.getExternalStorageDirectory(),返回的存储目录并非系统内置的SD卡目录。
我又换了一个 Sony L39u,一个 MOTO G,调用Environment.getExternalStorageDirectory()返回的目录就是系统内置的SD卡目录。
不一样的设备上,调用getExternalStorageDirectory()返回值却不同。查询了Android的文档,才找到缘由,原来这个方法返回的是当前设备厂商所认为的“外部存储”,有可能返回外置的SD卡目录(Micro SD Card),也可能返回内置的存储目(eMMC)。
总结一下:
一部分手机将eMMC存储挂载到 /mnt/external_sd 、/mnt/sdcard2 等节点,而将外置的SD卡挂载到 Environment.getExternalStorageDirectory()这个结点。
此时,调用Environment.getExternalStorageDirectory(),则返回外置的SD的路径。
而另外一部分手机直接将eMMC存储挂载在Environment.getExternalStorageDirectory()这个节点,而将真正的外置SD卡挂载到/mnt/external_sd、/mnt/sdcard2 等节点。
此时,调用Environment.getExternalStorageDirectory(),则返回内置的SD的路径。
至此就能解释为都是无外置SD卡的状况下,在中兴手机上,调用
打印 Environment.getExternalStorageState(),却返回 ”removed“,在索尼、MOTO G上就返回:“mounted”
缘由已经知道了,但是如何在无外置SD卡的时候,获取到这个内置eMMC存储的具体路径呢?
好比,我这个中兴手机,既然使用 Environment.getExternalStorageDirectory() 获取到的是外置SD卡路径,可是我又没有插入SD卡,这个时候我想使用内置的eMMC存储来存储一些程序中用到的数据,我怎么去获取这个eMMC存储的路径呢?
答案是:经过扫描系统文件"system/etc/vold.fstab”来实现。
"system/etc/vold.fstab” 只是一个简单的配置文件,它描述了Android的挂载点信息。
咱们能够遍历这个文件来获取全部的挂载点:
html
/** * 遍历 "system/etc/vold.fstab” 文件,获取所有的Android的挂载点信息 * * @return */ private static ArrayList<String> getDevMountList() { String[] toSearch = FileUtils.readFile("/etc/vold.fstab").split(" "); ArrayList<String> out = new ArrayList<String>(); for (int i = 0; i < toSearch.length; i++) { if (toSearch[i].contains("dev_mount")) { if (new File(toSearch[i + 2]).exists()) { out.add(toSearch[i + 2]); } } } return out; }
以后,当 Environment.getExternalStorageState()返回“removed”的时候(即,当没有挂载外置SD卡的时候),经过getDevMountList()方法获取一个list,这个list中能够进行写操做的那个就是系统自带的eMMC存储目录了。
判断逻辑:java
/** * 获取扩展SD卡存储目录 * * 若是有外接的SD卡,而且已挂载,则返回这个外置SD卡目录 * 不然:返回内置SD卡目录 * * @return */ public static String getExternalSdCardPath() { if (SDCardUtils.isMounted()) { File sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); return sdCardFile.getAbsolutePath(); } String path = null; File sdCardFile = null; ArrayList<String> devMountList = getDevMountList(); for (String devMount : devMountList) { File file = new File(devMount); if (file.isDirectory() && file.canWrite()) { path = file.getAbsolutePath(); String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date()); File testWritable = new File(path, "test_" + timeStamp); if (testWritable.mkdirs()) { testWritable.delete(); } else { path = null; } } } if (path != null) { sdCardFile = new File(path); return sdCardFile.getAbsolutePath(); } return null; }
参考:
[1] http://hubingforever.blog.163.com/blog/static/17104057920129198236599/
[2] http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location
[3] http://developer.android.com/about/versions/android-3.0.html
android