zoukankan      html  css  js  c++  java
  • Sorageinfo类获得Android设备上挂载的所有存储器状态

    获取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<StorageInfo> listAvaliableStorage(Context context) {
            ArrayList<StorageInfo> storagges = new ArrayList<StorageInfo>();
            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;
        }
    }
    


  • 相关阅读:
    CompositeConfiguration的用法
    Intellij IDEA连接Git@OSC
    Spring4.1.6 标签注解大全以及解释
    React制作吸顶功能总结
    总结常见的ES6新语法特性。
    12个常规前端面试题及小结
    JavaScript 扩展运算符
    Promise,Generator(生成器),async(异步)函数
    Vue中过度动画效果应用
    vue的双向绑定原理及实现
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768444.html
Copyright © 2011-2022 走看看