
获取android的所有挂载路径(转)
发布日期:2021-05-08 18:11:47
浏览次数:15
分类:原创文章
本文共 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; }
发表评论
最新留言
很好
[***.229.124.182]2025年04月09日 10时29分07秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
采坑 - 字符串的 "" 与 pd.isnull()
2019-03-06
无序列表 - 链表
2019-03-06
SQL 查询强化 - 数据准备
2019-03-06
SQL 强化练习 (四)
2019-03-06
SQL 强化练习 (八)
2019-03-06
Excel 拼接为 SQL 并打包 exe
2019-03-06
Pandas数据分析从放弃到入门
2019-03-06
Matplotlib绘制漫威英雄战力图,带你飞起来!
2019-03-06
机器学习是什么
2019-03-06
《小王子》里一些后知后觉的道理
2019-03-06
《自私的基因》总结
2019-03-06
《山海经》总结
2019-03-06
《非暴力沟通》总结
2019-03-06
《你当像鸟飞往你的山》总结
2019-03-06
《我是猫》总结
2019-03-06
《抗糖化书》总结
2019-03-06
apache虚拟主机配置
2019-03-06
光盘作为yum源
2019-03-06
PHP 正则表达式资料
2019-03-06
PHP官方网站及PHP手册
2019-03-06