android6.0通过反射获取USB和U盘以及内部存储路径,区分USB和U盘路径
2017-04-26 20:35
543 查看
------keyword是用来区分返回的是USB路径还是内部存储路径还是U盘路径
------主要还是storageVolume类(代表一个外挂设备)中的getUserLabel 这个方法,我们反射这个方法获取到的这个是当前存储设备的名称
public static String SD = "内部存储";
public static String EXT = "SD";
public static String USB = "盘";
----这三个便是传参数时keyword
public static String getStoragePath(Context mContext,String keyword) {
String resultpath = "";
StorageManager mStorageManager = (StorageManager) mContext
.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
// Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
// Method[] getDescriptionId = storageVolumeClazz.getDeclaredMethods();
// String content = "";
// for (int i = 0; i < getDescriptionId.length; i++) {
// Method method = getDescriptionId[i];
// String methodmname = method.getName();
// content += methodmname + "<><> " + "\r\n";
// }
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
Method getUserLabel = storageVolumeClazz.getMethod("getUserLabel");
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String userLabel = (String) getUserLabel.invoke(storageVolumeElement);
// content += userLabel + "<><> " + "\r\n";
String path = (String) getPath.invoke(storageVolumeElement);
if(userLabel.contains(keyword)){
resultpath = path;
}
// content += path + "<><> " + userLabel + "\r\n";
// boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
//
// if (is_removale == removable) {
//
// return path;
// }
}
// writeFile(content);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return resultpath;
}
//这个写文件的方法是测试的时候将反射获取的外置路径以及外置卡信息写入文件中方便我们查看
public static void writeFile(String content) {
try {
PrintStream ps = new PrintStream(new FileOutputStream(new File(
"/mnt/sdcard/path.txt")));
ps.append(content);// 往文件里写入字符串
ps.flush();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
------主要还是storageVolume类(代表一个外挂设备)中的getUserLabel 这个方法,我们反射这个方法获取到的这个是当前存储设备的名称
public static String SD = "内部存储";
public static String EXT = "SD";
public static String USB = "盘";
----这三个便是传参数时keyword
public static String getStoragePath(Context mContext,String keyword) {
String resultpath = "";
StorageManager mStorageManager = (StorageManager) mContext
.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
// Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
// Method[] getDescriptionId = storageVolumeClazz.getDeclaredMethods();
// String content = "";
// for (int i = 0; i < getDescriptionId.length; i++) {
// Method method = getDescriptionId[i];
// String methodmname = method.getName();
// content += methodmname + "<><> " + "\r\n";
// }
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
Method getUserLabel = storageVolumeClazz.getMethod("getUserLabel");
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String userLabel = (String) getUserLabel.invoke(storageVolumeElement);
// content += userLabel + "<><> " + "\r\n";
String path = (String) getPath.invoke(storageVolumeElement);
if(userLabel.contains(keyword)){
resultpath = path;
}
// content += path + "<><> " + userLabel + "\r\n";
// boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
//
// if (is_removale == removable) {
//
// return path;
// }
}
// writeFile(content);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return resultpath;
}
//这个写文件的方法是测试的时候将反射获取的外置路径以及外置卡信息写入文件中方便我们查看
public static void writeFile(String content) {
try {
PrintStream ps = new PrintStream(new FileOutputStream(new File(
"/mnt/sdcard/path.txt")));
ps.append(content);// 往文件里写入字符串
ps.flush();
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- Android 6.0上sdcard和U盘路径获取和区分
- Android 6.0上sdcard和U盘路径获取和区分
- Android 6.0上sdcard和U盘路径获取和区分方法
- Android 通过反射调用获取内置存储和外置sd卡根路径(适用于各个版本)
- Android手机自带内部存储路径的获取
- android--关于内部存储、外部存储以及存储路径
- android6.0 外部存储设备插拔广播以及获取路径(U盘)
- Android外部存储/内部存储路径获取大全
- Android 内部存储(外置SD卡)和扩展存储卡(TF卡)路径的获取
- 彻底搞懂Android文件存储---内部存储,外部存储以及各种存储路径解惑
- 使用Runtime类 识别 sdcard和U盘android 6.0 存储路径识别
- android通过反射得到USB挂载路径
- android获取扩展存储路径,TF卡、U盘等
- android获取外部存储和内部存储路径
- Android通过反射的方法获取手机存储设备的状态信息
- 解决Android手机自带内部存储路径存在但是却获取不到内容的问题
- Android 通过文件管理器选择文件夹下面的文件以及获取路径
- android6.0 外部存储设备插拔广播以及获取路径(U盘)
- Android手机自带内部存储路径的获取
- android6.0 外部存储设备插拔广播以及获取路径(U盘)【转】