android 9.0 获取U盘路径
16lz
2021-01-23
/** * android 9.0获取外置sdcard和U盘路径,并区分 * * @param mContext * * @param keyword SD = "内部存储"; EXT = "SD卡"; USB = "U盘" * * @return * */public static String getStoragePath(Context mContext,String keyword){ String targetpath = ""; StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE); Class<?> storageVolumeClazz = null; try { storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); Method getPath = storageVolumeClazz.getMethod("getPath"); Object result = getVolumeList.invoke(mStorageManager); final int length = Array.getLength(result); Method getUserLabel = storageVolumeClazz.getMethod("getUserLabel"); for (int i = 0; i < length; i++){ Object storageVolumeElement = Array.get(result, i); String userLabel = (String) getUserLabel.invoke(storageVolumeElement); String path = (String) getPath.invoke(storageVolumeElement); if(userLabel.contains(keyword)){ targetpath = path; targetpath = targetpath +"/xxx"; File dirFile = new File(targetpath); if (!dirFile.exists()) { //如果不存在,那就建立这个文件夹 dirFile.mkdirs(); } } } } catch (ClassNotFoundException e){ e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return targetpath ; }
更多相关文章
- android保存Bitmap图片到指定文件夹示例
- Android判断有无外置SD卡(TF卡),并读写文件
- java拷贝文件夹和android设置文件权限
- 第二十篇 Android获取本机图片、音频、视频、文档以及本地文件夹
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
- Eclipse+Android解决SDK下无doc文件夹问题 以及关联Android帮助
- android 应用在SD卡创建应用的文件夹
- Android各国语言缩写以及各国语言Values文件夹命名规则
- Android SDK Manager在添加Platforms和其他Packages时提示无法在