/**
* android 9.0获取外置sdcard和U盘路径,并区分
* * @param mContext
* * @param keyword SD = "内部存储"; EXT = "SD卡"; USB = "U盘"
* * @return
* */
public static String getStoragePath(Context mContext,String keyword){
String targetpath = "";
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");
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);
String path = (String) getPath.invoke(storageVolumeElement);
if(userLabel.contains(keyword)){
targetpath = path;
targetpath = targetpath +"/xxx";
File dirFile = new File(targetpath);
if (!dirFile.exists()) { //如果不存在,那就建立这个文件夹
dirFile.mkdirs();
}
}
}
} catch (ClassNotFoundException e){
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return targetpath ;
}