方法1 storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getPath = null; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { getPath = storageVolumeClazz.getMethod("getPath"); } List<android.os.storage.StorageVolume> volumes = storageManager.getStorageVolumes(); path = new String[volumes.size()]; for (int i = 0; i < volumes.size(); i++) { android.os.storage.StorageVolume storageVolume = volumes.get(i); String storagePath = ""; String storagePath = (String) getPath.invoke(storageVolume); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { storagePath = storageVolume.getDirectory().getAbsolutePath(); } else { storagePath = (String) getPath.invoke(storageVolume); } path[i]=(storagePath); } } catch (Exception e) { Log.e(tag, e.getMessage()); }
方法2
private static final String ENV_SECONDARY_STORAGE = "SECONDARY_STORAGE"; public static List<String> getExtendedMemoryPath(Context mContext) { List<String> paths = new ArrayList<String>(); File sdCard = Environment.getExternalStorageDirectory(); paths.add(sdCard.getAbsolutePath()); final String rawSecondaryStorage = System.getenv(ENV_SECONDARY_STORAGE); if (!TextUtils.isEmpty(rawSecondaryStorage)) { String[] externalCards = rawSecondaryStorage.split(":"); for (int i = 0; i < externalCards.length; i++) { String path = externalCards[i]; paths.add(path); } } return paths; }