Skip to content Skip to sidebar Skip to footer

Android Get All External Storage Path For All Devices

getExternalStorageDirectory() return SD card path on my phone. (Huawei Y320 - android 4.2.2). now, how to get path Phone Storage path for all devices and all API? loot at bottom sc

Solution 1:

External directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

if you User data directory...

Environment.getDataDirectory()

Recommended reading : Android External Storage

Cheers!


Solution 2:

try this code to get all external storage path for all devices

File[] f = ContextCompat.getExternalFilesDirs(getApplicationContext(),null);
for (int i=0;i< f.length;i++)
{
  String path = f[i].getParent().replace("/Android/data/","").replace(getPackageName(),"");
  Log.d("DIRS",path); //sdcard and internal and usb
}

Solution 3:

Android 21+:

val publicStorages = ContextCompat.getExternalFilesDirs(this, null).mapNotNull {
    it?.parentFile?.parentFile?.parentFile?.parentFile
}

// paths:
// /storage/emulated/0 
// /storage/12F7-270F

Android 29+:

val volumeNames = MediaStore.getExternalVolumeNames(context).toTypedArray()

val phoneSdCard: String = volumeNames[0]
// external_primary == /storage/emulated/0

val removableMicroSdCard: String = volumeNames[1]
// 12f7-270f == /storage/12F7-270F

more about 29+: MediaStore alternative for deprecated Context.externalMediaDirs?


Solution 4:

I'm using this method:

    public static final String SD_CARD = "sdCard";
    public static final String EXTERNAL_SD_CARD = "externalSdCard";
    private static final String ENV_SECONDARY_STORAGE = "SECONDARY_STORAGE";

    public static Map<String, File> getAllStorageLocations() {
        Map<String, File> storageLocations = new HashMap<>(10);
        File sdCard = Environment.getExternalStorageDirectory();
        storageLocations.put(SD_CARD, sdCard);
        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];
                storageLocations.put(EXTERNAL_SD_CARD + String.format(i == 0 ? "" : "_%d", i), new File(path));
            }
        }
        return storageLocations;
    }

Post a Comment for "Android Get All External Storage Path For All Devices"