博客
关于我
获取android的所有挂载路径(转)
阅读量:389 次
发布时间:2019-03-05

本文共 2891 字,大约阅读时间需要 9 分钟。

方法一:
public static List<StorageInfo> listAllStorage(Context context) {        ArrayList<StorageInfo> storages = new ArrayList<StorageInfo>();        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);        try {            Class<?>[] paramClasses = {};            Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);            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);                    Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);                    String state = (String) getVolumeState.invoke(storageManager, info.path);                    info.state = state;                    Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);                    info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue();                    if (info.state.equals(CommonUtil.MOUNTED)) {                        storages.add(info);                    }                }            }        } catch (Exception e) {            e.printStackTrace();        }        storages.trimToSize();        return storages;    }


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);    }    @Override    public String toString() {        return "StorageInfo [path=" + path + ", state=" + state                + ", isRemoveable=" + isRemoveable + "]";    }}

方法二:

 /**            * 得到所有的存储路径(内部存储+外部存储)        *        * @param context    * @return            */    public static String[] getAllSdPaths(Context context) {        Method mMethodGetPaths = null;        String[] paths = null;        //通过调用类的实例mStorageManager的getClass()获取StorageManager类对应的Class对象        //getMethod("getVolumePaths")返回StorageManager类对应的Class对象的getVolumePaths方法,这里不带参数        StorageManager mStorageManager = (StorageManager)context                .getSystemService(context.STORAGE_SERVICE);//storage        try {            mMethodGetPaths = mStorageManager.getClass().getMethod("getVolumePaths");            paths = (String[]) mMethodGetPaths.invoke(mStorageManager);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return paths;    }

转载地址:http://ebbzz.baihongyu.com/

你可能感兴趣的文章
delete对象时会自动调用类的析构函数
查看>>
C++ 子类对象直接赋值给父类对象可行,反过来不行
查看>>
linux下同一个动态库名为何辣么多的.so文件
查看>>
SQL联表的方式(逗号, Left Join, Right Join)
查看>>
牛客网输入输出举例
查看>>
字符串初始化时的注意点
查看>>
软考相关试题
查看>>
顺序表的操作
查看>>
常量表达式
查看>>
POD类型
查看>>
const与常量,傻傻分不清楚~
查看>>
Head First设计模式——迭代器模式
查看>>
MongoDB版本及存储引擎区别
查看>>
shell echo单行和多行文字定向写入到文件中
查看>>
AtCoder Beginner Contest 100 题解
查看>>
【数据结构】可持久化线段树初步
查看>>
Java高性能编程之CAS与ABA及解决方法
查看>>
从BIO到Netty的演变
查看>>
《算法导论》第二章笔记
查看>>
HTML节点操作
查看>>