-2

I want to delete duplicate files from SD Card. So i need to access SD Card files which give me the full path. eg. /storage/0C08-291C/Android/Docs/file-sample_500kB.rtf

I got the SD Card path for all Android version greater than SDK level 22 but not able to get SD Card access on Android 8 Oreo Version 8.0 and Version 8.1 SDK level 26 and 27.

I am using below method to get SD Card root path:

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;
}

This method return null on Android Version 8 even if SD card mounted status is true.

I am checking SD Card status by using below line:

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

And getting the SD Card path as:

String externalSdCard = getExternalStoragePath(mContext, true);

Once i got the externalSdCard i am calling the below method to GRANT URI PERMISSION to SD Card.

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();
        }
    }
}

To call the takeCardUriPermission(String sdCardRootPath) method i required SD Card root path. and my question is this "How to get root path of SD Card on Android 8 Oreo"

  • Could you please share the analysis of your issues done from your end i.e. code snippet, exact error so community could advice on this. – Raj Paliwal Jan 14 '20 at 09:47
  • @RajPaliwal I used the method (getExternalStoragePath) from [link](https://stackoverflow.com/questions/57037082/how-to-get-my-app-write-on-sd-card-with-oreo) This method give null string response on Oreo device if i pass is_removable value to true. – Chetan Patil Jan 14 '20 at 10:17
  • Post the used code in your post. (And not in links or comments). – blackapps Jan 14 '20 at 11:25
  • [@Raj Paliwal](https://stackoverflow.com/users/6550782/raj-paliwal) I got this method from [https://stackoverflow.com/questions/57037082/how-to-get-my-app-write-on-sd-card-with-oreo](https://stackoverflow.com/questions/57037082/how-to-get-my-app-write-on-sd-card-with-oreo) It is working for Android version >22. but not able to get SdCard path on Android 8 Oreo. – Chetan Patil Jan 15 '20 at 05:13

2 Answers2

-1

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

Environment.getExternalStorageDirectory()

Note: From Android Q on this method is deprecated.

mobo
  • 405
  • 3
  • 7
  • And on no Android version that will give a path to a removable micro SD card. Never has done. – blackapps Jan 14 '20 at 11:26
  • [@blackapps](https://stackoverflow.com/users/12121419/blackapps) Once check my above method in answer. It works well for all android version >22 but not working for Android 8 Oreo. – Chetan Patil Jan 15 '20 at 04:42
-1

@blackapps Below is my code to get the sd card root path.

Activity:

String externalSdCard = getExternalStoragePath(mContext, true); ... here externalSdCard, i am getting null for Android 8 Oreo SDK level 26 and 27. Other SDK level >=22 working well.

Method:

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;
}