@SuppressLint("PrivateApi")
private String getStoragePath(Context context, boolean isUsb){
String path="";
StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
Class<?> volumeInfoClazz;
Class<?> diskInfoClaszz;
try {
volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo");
diskInfoClaszz = Class.forName("android.os.storage.DiskInfo");
Method StorageManager_getVolumes=Class.forName("android.os.storage.StorageManager").getMethod("getVolumes");
Method VolumeInfo_GetDisk = volumeInfoClazz.getMethod("getDisk");
Method VolumeInfo_GetPath = volumeInfoClazz.getMethod("getPath");
Method DiskInfo_IsUsb = diskInfoClaszz.getMethod("isUsb");
Method DiskInfo_IsSd = diskInfoClaszz.getMethod("isSd");
List<Object> List_VolumeInfo = (List<Object>) StorageManager_getVolumes.invoke(mStorageManager);
assert List_VolumeInfo != null;
for(int i=0; i<List_VolumeInfo.size(); i++){
Object volumeInfo = List_VolumeInfo.get(i);
Object diskInfo = VolumeInfo_GetDisk.invoke(volumeInfo);
if(diskInfo==null)continue;
boolean sd= (boolean) DiskInfo_IsSd.invoke(diskInfo);
boolean usb= (boolean) DiskInfo_IsUsb.invoke(diskInfo);
File file= (File) VolumeInfo_GetPath.invoke(volumeInfo);
if(isUsb == usb){//usb
assert file != null;
path=file.getAbsolutePath();
}else if(!isUsb == sd){//sd
assert file != null;
path=file.getAbsolutePath();
}
}
} catch (Exception e) {
YYLog.print(TAG, "[——————— ——————— Exception:"+e.getMessage()+"]");
e.printStackTrace();
}
return path;
}