安卓6.0如何获取外置sd卡路径

2025-10-01 09:26:43

1、直接使用如下方法即可:

2、private static String getStoragePath(Context mContext, boolean is_removale) {    

  

      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");  

            Method isRemovable = storageVolumeClazz.getMethod("isRemovable");  

            Object result = getVolumeList.invoke(mStorageManager);  

            final int length = Array.getLength(result);  

            for (int i = 0; i < length; i++) {  

                Object storageVolumeElement = Array.get(result, i);  

                String path = (String) getPath.invoke(storageVolumeElement);  

                boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);  

                if (is_removale == removable) {  

                    return path;  

                }  

            }  

        } catch (ClassNotFoundException e) {  

            e.printStackTrace();  

        } catch (InvocationTargetException e) {  

            e.printStackTrace();  

        } catch (NoSuchMethodException e) {  

            e.printStackTrace();  

        } catch (IllegalAccessException e) {  

            e.printStackTrace();  

        }  

        return null;  

}  

3、好啦,以上就是安卓6.0获取外置sd卡路径的方法,赶快去试试吧!

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢