简体   繁体   中英

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

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.

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.

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;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM