32

I've been Googling around but it is sooooo hard to find what manufacturers/models use which path for sdcard/external storage.

  • I am NOT talking about the internal storage path which can be found by:

Environment.getExternalStorageDirectory()

I know that getExternalStorageDirectory() sometimes points to external sdcard on some devices.

Here's what I've found some common path for external path (Not sure which manufacturer uses which path):

/emmc
/mnt/sdcard/external_sd
/mnt/external_sd
/sdcard/sd
/mnt/sdcard/bpemmctest
/mnt/sdcard/_ExternalSD
/mnt/sdcard-ext
/mnt/Removable/MicroSD
/Removable/MicroSD
/mnt/external1
/mnt/extSdCard
/mnt/extsd
/mnt/usb_storage  <-- usb flash mount
/mnt/extSdCard  <-- usb flash mount
/mnt/UsbDriveA  <-- usb flash mount
/mnt/UsbDriveB  <-- usb flash mount

These are what I found by Googling around.

I need to scan entire internal + external storage + USB flash drive to look for a certain file. If I am missing any path, please add to the above list. If someone knows paths used by each manufacturers, please share with us.

jclova
  • 5,466
  • 16
  • 52
  • 78
  • Note that getExternalStorageDirectory() does not return the "internal storage" however it may return a storage which is internal, and possibly even a virtual partition implemented within the internal storage, but with access rights matching the External Storage concept. As for your question, you might check /proc/mounts however be aware that on some recent devices, parts of removable media where another system might have written may simply no longer be accessible to applications at all. – Chris Stratton Dec 20 '12 at 17:01
  • 2
    https://github.com/jow-ct/Environment2 code is a bit german but it tries to parse `/etc/vold.fstab` which contains all the real external storages – zapl Dec 20 '12 at 17:05
  • Hi zapl, /etc/void.fstab or /etc/void.config is real good place to look at where external storages are located. Thank you. However, Galaxy Nexus doesn't have neither files, which is OK since it doesn't have external micro-sd slot. But I am concern that relying on void.fstab is stable for every device. – jclova Dec 20 '12 at 20:46
  • 1
    @jclova I am trying to figure out the same, since you seem to have researched a bit more on this topic, do you think the android media scanner can be invoked on these paths ? In my case media scanner would only scan if provided with getExternalStorage() – Ahmed Jan 27 '13 at 23:21
  • @Ahmed, Sorry I didn't research the behavior of the media scanner. – jclova Mar 05 '13 at 19:13

4 Answers4

13

Good news! In KitKat there's now a public API for interacting with these secondary shared storage devices.

The new Context.getExternalFilesDirs() and Context.getExternalCacheDirs() methods can return multiple paths, including both primary and secondary devices. You can then iterate over them and check Environment.getStorageState() and File.getFreeSpace() to determine the best place to store your files. These methods are also available on ContextCompat in the support-v4 library.

Also note that if you're only interested in using the directories returned by Context, you no longer need the READ_ or WRITE_EXTERNAL_STORAGE permissions. Going forward, you'll always have read/write access to these directories with no additional permissions required.

Apps can also continue working on older devices by end-of-lifing their permission request like this:

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
Jeff Sharkey
  • 2,473
  • 1
  • 17
  • 10
11

I also started with a list like yours from somewhere (sorry couldn't find the original source) and continue to add some paths collected from user feedback. There are duplicate to the above list.

    /storage/sdcard1 //!< Motorola Xoom
    /storage/extsdcard  //!< Samsung SGS3
    /storage/sdcard0/external_sdcard  // user request
    /mnt/extsdcard
    /mnt/sdcard/external_sd  //!< Samsung galaxy family
    /mnt/external_sd
    /mnt/media_rw/sdcard1   //!< 4.4.2 on CyanogenMod S3
    /removable/microsd              //!< Asus transformer prime
    /mnt/emmc
    /storage/external_SD            //!< LG
    /storage/ext_sd                 //!< HTC One Max
    /storage/removable/sdcard1      //!< Sony Xperia Z1
    /data/sdext
    /data/sdext2
    /data/sdext3
    /data/sdext4
    /sdcard1                       //Sony Xperia Z
    /sdcard2                       //HTC One M8s
    /storage/microsd               //ASUS ZenFone 2

For what you want to achieve (scanning all possible paths for certain files), I think it's quite impossible to do it this way (using a list of known paths). Some paths on same device even changed between android upgrade.

Fedor
  • 43,261
  • 10
  • 79
  • 89
oceanuz
  • 171
  • 1
  • 6
  • Thank you for this solution, I have added it into my app. Unfortunately, this behavior caused problems: on Sony Xperia Z1 after upgrade to Android 4.4.2 the path /storage/removable/sdcard1 became read-only! More info here: http://www.androidpolice.com/2014/02/17/external-blues-google-has-brought-big-changes-to-sd-cards-in-kitkat-and-even-samsung-may-be-implementing-them/ So, I am going to use Context#getExternalFilesDir() – Martin Vysny Apr 20 '14 at 18:31
  • 1
    From 4.4 onwards, there are "official" ways to get the external storage directory (via context.getExternalFilesDirs("external")). Therefore, such a workaround is required only for lower versions of Android. – Jörg Eisfeld Jan 04 '16 at 18:18
  • 1
    @JörgEisfeld but `context.getExternalFileDirs` get application specific path only, not the root – HendraWD Nov 14 '16 at 05:35
  • what about `/storage/sdcard0`? – HendraWD Nov 14 '16 at 05:36
  • XiaoMi Tv box gen 1 found that is `/storage/external_storage/sda1` – Johnny Dec 16 '17 at 02:10
  • @JörgEisfeld: There are official ways to get external storage directories, but the OP asked for the SD card path (though the question is a little muddled). They are often not the same thing. So you have to filter the external storage directories, checking which ones are removable. Starting at 5.0, `Environment.isExternalStorageRemovable()` can take a directory argument. – LarsH Mar 22 '18 at 11:51
  • @HendraWD: Have you found an example where that path is used? – LarsH Mar 22 '18 at 11:54
  • @LarsH in my previous phone i think, but i don't remember it clearly. If you have OnePlus One phone model, the path might be like that. – HendraWD Mar 24 '18 at 03:43
0

Based on the paths oceanuz provided, I wrote a method, which locates the external storage path (ignoring the case) and returns it as a String.

Note: I used StreamSupport library inside my method, so in order for it to work, you'll need to download the jar file and add that to libs folder in your project and that's it.

  public static String getExternalSdPath(Context context) {
    List<String> listOfFoldersToSearch = Arrays.asList("/storage/", "/mnt/", "/removable/", "/data/");
    List<String> listOf2DepthFolders = Arrays.asList("sdcard0", "media_rw", "removable");
    List<String> listOfExtFolders = Arrays.asList("sdcard1", "extsdcard", "external_sd", "microsd", "emmc", "ext_sd", "sdext",
            "sdext1", "sdext2", "sdext3", "sdext4");
    final String[] thePath = {""};
    Optional<File> optional = StreamSupport.stream(listOfFoldersToSearch)
            .filter(s -> {
                File folder = new File(s);
                return folder.exists() && folder.isDirectory();
            }) //I got the ones that exist and are directories
            .flatMap(s -> {
                try {
                    List<File> files = Arrays.asList(new File(s).listFiles());
                    return StreamSupport.stream(files);
                } catch (NullPointerException e) {
                    return StreamSupport.stream(Collections.emptyList());
                }
            }) //I got all sub-dirs of the main folders
            .flatMap(file1 -> {
                if (listOf2DepthFolders.contains(file1.getName().toLowerCase())) {
                    try {
                        List<File> files = Arrays.asList(file1.listFiles());
                        return StreamSupport.stream(files);
                    } catch (NullPointerException e) {
                        return StreamSupport.stream(Collections.singletonList(file1));
                    }
                } else
                    return StreamSupport.stream(Collections.singletonList(file1));
            }) //Here I got all the 2 depth and 3 depth folders
            .filter(o -> listOfExtFolders.contains(o.getName().toLowerCase()))
            .findFirst();

    optional.ifPresent(file -> thePath[0] = file.getAbsolutePath());

    Log.i("Path", thePath[0]);

    return thePath[0];
}

This method will return an empty string if there is no Sd Card path found.

sudo_rizwan
  • 584
  • 5
  • 10
0

I was trying to find path to my external SD card on Sony Xperia XA. I then installed ES File explorer and that clearly showed path to any folder, internal or external, in it's characteristics window. I found the path to external SD as /storage/C4EE-F3A3. I was trying to save output file from Python3 to external SD but it did not have permission to save anywhere other than Qpython folder in the internal SD.