Android手机自带内部存储路径的获取

☆樱花仙子☆ 提交于 2019-12-09 12:26:38

我有一台中兴的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的挂载点信息。
我们可以遍历这个文件来获取所有的挂载点:

    /**
	 * 遍历 "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存储目录了。

判断逻辑:

    /**
	 * 获取扩展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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!