How to get the path of removable storage/sd card? Latest (/storage/???? ????) format as well

366 views Asked by At

How to get the path of removable storage (removable SD Card) on Android 10, especially samsung phones. As the location of the removable storage is now of the format /storage/???? ???? I found it hard to get the path as it changes with each phone.

I am posting the solution as well, it has worked well on Samsung and Mi phones.

2

There are 2 answers

0
Saqib On

I would like to share a solution which took me days to find and hopefully shall help others as well. The removable sd card storage on latest android versions is like "/storage/???? ????", this location is quite hard to get. I found the following code from here: This code helps get the correct location even on Samsung phones. I have tried it on Samsung and Mi phones.

/**
  • Get external sd card path using reflection

  • @param mContext

  • @param is_removable is external storage removable

  • @return */

     private 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 e) {
         e.printStackTrace();
     } catch (InvocationTargetException e) {
         e.printStackTrace();
     } catch (NoSuchMethodException e) {
         e.printStackTrace();
     } catch (IllegalAccessException e) {
         e.printStackTrace();
     }
     return null;
    

    }

0
blackapps On

On every phone or emulator you get the path to app specific folder on removable micro sd card by using the second item returned by

getExternalFilesDirs()

The path is different for every micro sd card used.