3

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 screenshot.

enter image description here

Farzad
  • 1,975
  • 1
  • 25
  • 47
  • 1
    Below Android 5.0 there is no reliable method to determine the path to a removable sd card. It differs from device to device and sometimes it is impossible. For above you should try `getExternalFilesDirs()`. It will give you two paths. The second one is on the card. – greenapps Oct 04 '16 at 11:05
  • 1
    By the way: You picture is wrong. getExternalStorageDirectory does not deliver 'SD card' but 'Phone storage'. – greenapps Oct 04 '16 at 11:06
  • 1
    So i realise that i read your post wrong from the beginning. I was confused as mostly the question is how to get the path to the sd card. But what you say is impossible. `getExternalStorageDirectory()` will deliver a path to `Phone storage`. Often that is `/storage/emulated/0` or `/mnt/sdcard` or `/sdcard`. Please tell what you get. – greenapps Oct 04 '16 at 11:09
  • @greenapps thank you for comment, i need create folder (mkdir) into sdcard, but if sdcard no space for save filem so create folder to another storage (phone storage) for save file. – Farzad Oct 04 '16 at 11:13
  • Why are you giving such irrelevant info in a comment? Please react on what i said and asked! And keep to your problem. – greenapps Oct 04 '16 at 11:17
  • @greenapps i don't know, please help me, You just answer me this question: `getExternalStorageDirectory()` is work for all devices and API and exists always? (if mounted) – Farzad Oct 04 '16 at 11:24
  • 1
    That works for all api's and all devices. And exists always. It never is unmounted. And it always delivers external memory. And never a path to SD card. Except for old devices with Android 2.0 and 3.0. There it delivers indeed sometimes a path to a removable SD card. (I still have two such devices). – greenapps Oct 04 '16 at 11:29
  • @greenapps thank you, please answer another question --> Which is correct for save file? `getExternalStorageDirectory().getPath()."/exm.mp3"` or `getExternalStorageDirectory().getAbsolutePath()."/exm.mp3"` or `getExternalStorageDirectory().toString()."/exm.mp3"` ? – Farzad Oct 04 '16 at 11:35

4 Answers4

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?

user924
  • 8,146
  • 7
  • 57
  • 139
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
}
milan pithadia
  • 840
  • 11
  • 16
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!

O_o
  • 1,103
  • 11
  • 36
  • in my screenshot **Phone storage** is external or internal? (I do not want remove files after unistall app !!!) – Farzad Oct 04 '16 at 10:41
  • 1
    @grizzly, That depends. In some android devices, you won't be able to insert your SD card (External Storage), where phone storage is the only storage and act as external devices (preferrably devices with 8gb or more internal storage).. you need to use your code to distinguish whether you have external storage and if so, what are you going to do, if not, what path will you take.. – O_o Oct 04 '16 at 10:43
  • ok, In your opinion, @Fedor Kazakov answer is true and fix for all devices? – Farzad Oct 04 '16 at 10:49
  • 1
    In my opinion, this answer is true and fix your SD card less or available problem.. http://stackoverflow.com/a/21724952/5876080 and Environment comes with OS. It's same across devices. @grizzly – O_o Oct 04 '16 at 10:58
0

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;
    }
Fedor Kazakov
  • 601
  • 3
  • 12
  • this method is fix on all device and API? (samsung,sony,huawei, etc...) ?? – Farzad Oct 04 '16 at 10:43
  • `sdCard` and `externalSdCard` and `SECONDARY_STORAGE` names is fixed and constant in all devies? for example: `sdCard` string fixed in huawei samsung sony ? or may Different example `sd_card` ? – Farzad Oct 04 '16 at 10:57
  • 1
    SECONDARY_STORAGE is system variable and it is constant – Fedor Kazakov Oct 04 '16 at 11:22
  • getExternalStorageDirectory() is work for all devices and API and exists always? (if mounted) (example android 6+) – Farzad Oct 04 '16 at 11:25