如何获取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设备挂载的所有存储器路径,以及存储器类型(内置存储还是外置存储),还有存储器的挂载状态等信息。

标签:Android,设备挂载,存储器
0
投稿

猜你喜欢

  • unity 如何使用LineRenderer 动态划线

    2021-10-27 03:42:50
  • Java使用动态规划算法思想解决背包问题

    2022-12-02 03:53:49
  • java图搜索算法之DFS与BFS详解

    2022-08-15 15:48:57
  • Android简单获取经纬度的方法

    2021-07-28 05:26:47
  • Android中手机录屏并转换GIF的两种方式

    2021-12-03 13:00:52
  • SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI

    2022-07-08 07:40:23
  • c语言重要的字符串与内存函数

    2023-04-28 00:35:42
  • 适配Android 8.0版本更新安装与通知栏的一些坑

    2022-05-01 13:23:25
  • 使用JDBC4.0操作XML类型的字段(保存获取xml数据)的方法

    2021-08-25 17:44:38
  • java自定义封装StringUtils常用工具类

    2022-09-01 05:11:13
  • Spring cloud网关gateway进行websocket路由转发规则配置过程

    2022-01-24 01:48:38
  • Android性能之冷启动优化详析

    2022-03-06 13:42:51
  • DUCC配置平台实现一个动态化线程池示例代码

    2023-11-28 12:07:39
  • springtask 的使用方法和 cron 表达式解析

    2023-06-19 20:05:56
  • 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)

    2022-11-09 11:40:37
  • Java JDK 二分法 分析demo(推荐)

    2022-02-28 23:29:21
  • C#画笔Pen绘制自定义线的帽子

    2022-01-09 20:33:55
  • MultipartFile中transferTo(File file)的路径问题及解决

    2023-11-12 00:07:08
  • Java设计模式之工厂模式案例详解

    2023-11-27 20:08:03
  • C#3.0中Lambda表达式详解

    2023-08-08 04:01:37
  • asp之家 软件编程 m.aspxhome.com