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

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

方法一:
public static List
listAllStorage(Context context) { ArrayList
storages = new ArrayList
(); 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/

你可能感兴趣的文章
Netty常用组件一
查看>>
Netty常见组件二
查看>>
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty心跳检测
查看>>
Netty心跳检测机制
查看>>
netty既做服务端又做客户端_网易新闻客户端广告怎么做
查看>>
Netty核心模块组件
查看>>
Netty框架内的宝藏:ByteBuf
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—1.服务端启动流程一
查看>>
Netty源码—1.服务端启动流程二
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—2.Reactor线程模型二
查看>>
Netty源码—3.Reactor线程模型三
查看>>
Netty源码—3.Reactor线程模型四
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—5.Pipeline和Handler二
查看>>
Netty源码—6.ByteBuf原理一
查看>>