Android 获取外部挂在的设备路径包括内部SD卡,外部SD卡,U盘的设备路径

Android 获取外部挂在的设备路径包括内部SD卡,外部SD卡,U盘的设备路径,第1张

Android 获取外部挂在的设备路径包括内部SD卡,外部SD卡,U盘的设备路径
 StorageManager mStorageManager;
        mStorageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
        //获取所有挂载的设备(内部sd卡、外部sd卡、挂载的U盘)
        List volumes = mStorageManager.getStorageVolumes();
        try {
            Class  storageVolumeClazz = Class
                    .forName("android.os.storage.StorageVolume");
            //通过反射调用系统hide的方法
            Method getPath = storageVolumeClazz.getMethod("getPath");
            Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
            for (int i = 0; i < volumes.size(); i++) {
                StorageVolume storageVolume = volumes.get(i);//获取每个挂载的StorageVolume

                //通过反射调用getPath、isRemovable
                String storagePath = (String) getPath.invoke(storageVolume); //获取路径
                boolean isRemovableResult = (boolean) isRemovable.invoke(storageVolume);//是否可移除
                String description = storageVolume.getDescription(this);
                LogUtil.e("Legejason i=" + i + " ,storagePath=" + storagePath
                        + " ,isRemovableResult=" + isRemovableResult +" ,description="+description);
            }
        } catch (Exception e) {
            LogUtil.e("Legejason e:" + e);
        }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5575503.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存