如何获取Android设备挂载的所有存储器
作者:黑月神话 时间:2023-08-16 12:44:41
android系统提供了Environment.getExternalStorageDirectory()接口获得存储器的路径,但是这个接口往往给的结果并不是我们想要的,在某些设备上它返回的是手机内部存储,某些设备它返回的手机外部存储。还有就是某些Android设备支持扩展多个sdcard,这个时候想要获得所有存储器的挂载路径,这个接口是没有办法办到的。
怎么获取Android设备所有存储器的位置呢?或者说获得所有的挂载点
系统提供了一个StorageManager,它有一个方法叫getVolumeList,这个方法的返回值是一个StorageVolume数组,StorageVolume类中封装了挂载路径,挂载状态,以及是否可以移除等等信息。但是很可惜,这个方法是隐藏的api,所以我们只能通过反射来调用这个方法了,下面是这个方法的源码。
public StorageVolume[] getVolumeList() {
if (mMountService == null) return new StorageVolume[0];
try {
Parcelable[] list = mMountService.getVolumeList();
if (list == null) return new StorageVolume[0];
int length = list.length;
StorageVolume[] result = new StorageVolume[length];
for (int i = 0; i < length; i++) {
result[i] = (StorageVolume)list[i];
}
return result;
} catch (RemoteException e) {
Log.e(TAG, "Failed to get volume list", e);
return null;
}
}
通过反射,获取到Android设备所有存储器。
public class StorageInfo {
public String path;
public String state;
public boolean isRemoveable;
public StorageInfo(String path) {
this.path = path;
}
public boolean isMounted() {
return "mounted".equals(state);
}
}
public static List listAvaliableStorage(Context context) {
ArrayList storagges = new ArrayList();
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
Class<?>[] paramClasses = {};
Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
getVolumeList.setAccessible(true);
Object[] params = {};
Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
if (invokes != null) {
StorageInfo info = null;
for (int i = 0; i < invokes.length; i++) {
Object obj = invokes[i];
Method getPath = obj.getClass().getMethod("getPath", new Class[0]);
String path = (String) getPath.invoke(obj, new Object[0]);
info = new StorageInfo(path);
File file = new File(info.path);
if ((file.exists()) && (file.isDirectory()) && (file.canWrite())) {
Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
String state = null;
try {
Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
state = (String) getVolumeState.invoke(storageManager, info.path);
info.state = state;
} catch (Exception e) {
e.printStackTrace();
}
if (info.isMounted()) {
info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue();
storagges.add(info);
}
}
}
}
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
storagges.trimToSize();
return storagges;
}
如何判断存储器是内置存储还是外置存储呢?
StorageVolume这个类中提供了一个isRemovable()接口,通过反射调用它就可以知道存储器是否可以移除。把可以移除的存储器认定为外置sdcard,不可移除的存储器认定为内置存储器。
Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
如何判断存储器的挂载状态呢?
同上面一样,需要反射系统接口才可以获取到挂载状态。下面是代码片段
Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
state = (String) getVolumeState.invoke(storageManager, info.path);
info.state = state;
总结
通过反射系统的StorageManager以及StorageVolume类提供的接口,就可以拿到Android设备挂载的所有存储器路径,以及存储器类型(内置存储还是外置存储),还有存储器的挂载状态等信息。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
unity 如何使用LineRenderer 动态划线
![](https://img.aspxhome.com/file/2023/9/102269_0s.jpg)
Java使用动态规划算法思想解决背包问题
![](https://img.aspxhome.com/file/2023/7/89497_0s.png)
java图搜索算法之DFS与BFS详解
![](https://img.aspxhome.com/file/2023/6/63026_0s.png)
Android简单获取经纬度的方法
Android中手机录屏并转换GIF的两种方式
![](https://img.aspxhome.com/file/2023/9/138029_0s.png)
SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI
![](https://img.aspxhome.com/file/2023/6/61516_0s.jpg)
c语言重要的字符串与内存函数
适配Android 8.0版本更新安装与通知栏的一些坑
![](https://img.aspxhome.com/file/2023/3/90143_0s.jpg)
使用JDBC4.0操作XML类型的字段(保存获取xml数据)的方法
java自定义封装StringUtils常用工具类
Spring cloud网关gateway进行websocket路由转发规则配置过程
Android性能之冷启动优化详析
![](https://img.aspxhome.com/file/2023/4/106864_0s.jpg)
DUCC配置平台实现一个动态化线程池示例代码
![](https://img.aspxhome.com/file/2023/4/60354_0s.png)
springtask 的使用方法和 cron 表达式解析
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
Java JDK 二分法 分析demo(推荐)
C#画笔Pen绘制自定义线的帽子
MultipartFile中transferTo(File file)的路径问题及解决
![](https://img.aspxhome.com/file/2023/5/80165_0s.png)
Java设计模式之工厂模式案例详解
![](https://img.aspxhome.com/file/2023/2/60342_0s.png)