Android 获取外部存储设备列表

来源:互联网 发布:程序员思维 编辑:程序博客网 时间:2024/05/30 05:40

由于不同芯片设备的外部存储设备不太统一,比如说外部sdcard,u盘等,他们的路径在不同芯片上面的定义是不一样的,所以我们需要动态的去获取机器当前的外部设备mount 的情况

Android 通过 StorageManager 类来管理存储设备的,这个类里面包含一些隐藏的方法:
getVolumeList —–> 返回StorageVolume 类型的数组,包含系统所有的存储设备volume
getVolumePaths—–> 返回String 类型的数组,包含系统所有存储设备的挂在路径

以下以getVolumeList 为例来获取系统所有的可插拔的设备

    priavte StorageManager mStorageManager;    private Method getStorageVolumes;    private Method getPath;    mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);        try {            getStorageVolumes = mStorageManager.getClass().getMethod("getVolumeList");            StorageVolume[] volumeList = (StorageVolume[]) getStorageVolumes.invoke(mStorageManager);            if(volumeList != null){                for(StorageVolume volume :volumeList){                    if(volume.isRemovable() && volume.getState().equals("mounted")){                        getPath = volume.getClass().getMethod("getPath");                        Log.d("MainActivity", "getPath.invoke(volume.getClass()):" + getPath.invoke(volume));                        String mountPoint = (String) getPath.invoke(volume);                    }                }            }        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        }
0 0
原创粉丝点击