如何获取奥利奥SD卡路径?

我想从SD卡中删除重复的文件。所以我需要访问SD卡文件,该文件为我提供了完整路径。例如。 /storage/0C08-291C/Android/Docs/file-sample_500kB.rtf

我获得了所有大于 SDK 22级的Android版本的SD卡路径,但无法在 Android 8 Oreo 8.0版中访问SD卡和版本8.1 SDK级别26和27

我正在使用以下方法获取SD卡的根路径:

public static String getExternalStoragePath(Context mContext,boolean is_removable) {
    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_removable == removable) {
                return path;
            }
        }
    } catch (ClassnotFoundException | invocationTargetException | NoSuchMethodException | IllegalaccessException e) {
        e.printStackTrace();
    }
    return null;
}

即使SD卡的安装状态为 true ,此方法在Android版本8上也会返回 null

我正在使用以下行检查SD卡的状态:

boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

并将SD卡路径获取为:

String externalSdCard = getExternalStoragePath(mContext,true);

一旦我获得了 externalSdCard ,我将调用以下方法将 GRANT URI PERMISSION 授予 SD卡

public void takeCardUriPermission(String sdCardRootPath) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        File sdCard = new File(sdCardRootPath);
        StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
        StorageVolume storageVolume = storageManager.getStorageVolume(sdCard);
        Intent intent = null;
        if (storageVolume != null) {
            intent = storageVolume.createaccessIntent(null);
        }
        try {
            startactivityForResult(intent,REQUEST_CODE_OPEN_DOCUMENT_TREE);
        } catch (activityNotFoundException e) {
            e.printStackTrace();
        }
    }
}

要调用 takeCardUriPermission(String sdCardRootPath)方法,我需要 SD卡根路径。我的问题是这个“如何在Android 8 Oreo上获取SD卡的根路径”

wwasdffdsa 回答:如何获取奥利奥SD卡路径?

https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

Environment.getExternalStorageDirectory()

注意:从Android Q上不赞成使用此方法。

,

@blackapps下面是我获取SD卡根路径的代码。

活动:

String externalSdCard = getExternalStoragePath(mContext,true); ...这里 externalSdCard ,对于 Android 8 Oreo SDK级别26和27 ,我得到的是空值。其他 SDK级别> = 22 效果很好。

方法:

public static String getExternalStoragePath(Context mContext,boolean is_removable) {
    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_removable == removable) {
                return path;
            }
        }
    } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
        e.printStackTrace();
    }
    return null;
}
本文链接:https://www.f2er.com/2772788.html

大家都在问