Android获取外接SD卡或者U盘路径方法

最近在开发Android时遇到插U盘获取U盘内容的需求,但是按照传统的Environment.getExternalStorageDirectory()只能读取到插入的SD卡的路径,如果是U盘的话无法读出U盘的路径。

最终在一个在CSDN的论坛里找到相关的东西,就试了下直接通过StorageManager获取存储路径的。

核心如下,volumePaths的数组就是系统外接设备的路径,经过测试的确是挂载的路径。不过有些是不可用的,它只是列出了系统可支持的外接路径。

final StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
String[] volumePaths = new String[0];
try {
	final Method method = sm.getClass().getMethod("getVolumePaths");
	if(null != method) {
		method.setAccessible(true);
		volumePaths = (String[]) method.invoke(sm);
	}
}catch (Exception e){
	e.printStackTrace();
}
if ((volumePaths != null) && (volumePaths.length > 0)){
	for (String sdcardPath : volumePaths){
		Log.d(TAG,"sdcardPath:" + sdcardPath);
	}
}

参考链接


android获取外接SD卡或者U盘路径方法

发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注