码迷,mamicode.com
首页 > 移动开发 > 详细

androidstudio安卓获取外部sd卡存储

时间:2020-09-03 16:56:55      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:rac   targe   not   boolean   get   dex   cat   trace   ems   

private static String getStoragePath(Context mContext, boolean is_removale) {

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");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String path = null;
try {
path = (String) getPath.invoke(storageVolumeElement);
} catch (InvocationTargetException e) {
e.printStackTrace();
}
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
if (is_removale == removable) {
return path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}

androidstudio安卓获取外部sd卡存储

标签:rac   targe   not   boolean   get   dex   cat   trace   ems   

原文地址:https://www.cnblogs.com/zyb2016/p/13561108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!