External SDCard file path for Android

Bowie picture Bowie · Apr 17, 2014 · Viewed 79.4k times · Source

Is it true that the file path to external SDCard on Android devices are always "/storage/extSdCard"? If not, how many variations are there?

I need it for my App to test the availability of external SDCard.

I am using Titanium, it has a method Titanium.Filesystem.isExternalStoragePresent( ) but it always return true even external SDCard is not mounted.

I think it detect SDCard at local storage thus return true. But what I really want is detect whether physical SDCard is mounted or not.

Can I do this by detecting the existence of file "/storage/extSdCard" alone?

Thanks.

Answer

Digoun picture Digoun · Apr 17, 2014

Is it true that the file path to external SDCard on Android devices are always "/storage/extSdCard"? If not, how many variations are there?

Sadly the path to the external storage is not always the same according to manufacturer. Using Environment.getExternalStorageDirectory() will return you the normal path for SD card which is mnt/sdcard/. But for Samsung devices for example, the SD card path is either under mnt/extSdCard/ or under mnt/external_sd/.

So one way to proceed would be to check the existence of external directory according to the path used by each manufacturer. With something like this:

mExternalDirectory = Environment.getExternalStorageDirectory()
            .getAbsolutePath();
    if (android.os.Build.DEVICE.contains("samsung")
            || android.os.Build.MANUFACTURER.contains("samsung")) {
        File f = new File(Environment.getExternalStorageDirectory()
                .getParent() + "/extSdCard" + "/myDirectory");
        if (f.exists() && f.isDirectory()) {
            mExternalDirectory = Environment.getExternalStorageDirectory()
                    .getParent() + "/extSdCard";
        } else {
            f = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/external_sd" + "/myDirectory");  
            if (f.exists() && f.isDirectory()) {
                mExternalDirectory = Environment
                        .getExternalStorageDirectory().getAbsolutePath()
                        + "/external_sd";
            }
        }
    }

But what I really want is detect whether physical SDCard is mounted or not.

I didn't try the code yet, but the approach of Dmitriy Lozenko in this answer is much more interesting. His method returns the path of all mounted SD cards on sytem regardless of the manufacturer.