0

I'm faced with the well-known problem of obtaining the path of an external SD card mounted on some Android devices. (see this question for understanding what I mean)

I've thought to solve the problem by reading the content of /etc/vold.fstab, then taking just lines representing partitions, but I don't have a device for doing tests. What I want to do is to read that file, ignore the row which refers to the address returned by Environment.getExternalStorageDirectory(), and take the other row (if present).

What I don't know (and I don't have the possibility to test it) is: are there cases in which I can have other lines which are not the external SD card? The SD card, if present, appears on the file vold.fstab?

edit: The answer is: YES. Read the accepted answer.

Community
  • 1
  • 1
Massimo
  • 3,436
  • 4
  • 40
  • 68
  • 1
    Environment.getExternalStorageDirectory() is the sdcard – njzk2 Nov 20 '12 at 15:34
  • Bear in mind that your solution may well not work on Android 4.2, given the multiple user account feature. It is unclear to what extent such volumes are accessible in general, let alone how 4.2 devices will treat them when being used by a secondary user. – CommonsWare Nov 20 '12 at 15:44
  • Why it should not work? (want to understand in order to find a possible solution) – Massimo Nov 20 '12 at 15:47
  • 1
    RE @njzk2: Environment.getExternalStorageDirectory() is NOT the sdcard, it is the build-in storage path. – rml Jun 25 '13 at 04:33
  • I think it should work on 4.2 also. Multiple users will have different sub-dirs in /home/, but /etc/vold.fstab must not change. Anyone tried that now? – Prahlad Yeri Sep 06 '13 at 09:00

3 Answers3

1

What is wrong with this?

Environment.getExternalStoreDirectory()

Why are you ignoring this when it's the SD Card?

OK - In the case of devices with /sdcard (Internal) and an external SD card (??) you could always scan the fstab file and look for "sdhci" which is the SD Host Controller bridge driver.

Something like:

dev_mount sdcard /mnt/external_sdcard auto /devices/platform/sdhci.2/mmc_host/mmc2

Then just parse as necessary.

Why the "necessity" to find the actual SD card though when it's not actually treated as such by the OS? (Won't be mounted as mass storage)

Is your application only available for devices where this is the case? What is wrong with using whatever Android believes is the SD storage space?

Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • I want to face with those devices which see the "external" memory (referred by Environment.getExternalStoreDirectory()) as a partition of the internal one and permit to insert another external sd-card. For these devices standard library does not have a method for obtaining the path of this external sd-card. – Massimo Nov 20 '12 at 15:37
  • What device are we talking about here? When a device doesn't have an SD slot, /sdcard is used on the internal drive. When there is an SD slot, /sdcard (Generally) is used as it's mount point. I've never heard of a device utilising both .... – Rawkode Nov 20 '12 at 15:41
  • Samsung Galaxy S3: http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0 – Massimo Nov 20 '12 at 15:44
  • Updated my answer. I believe you'll be able to scan for the Linux driver – Rawkode Nov 20 '12 at 15:50
  • I have to access this memory in a standard way because I am creating a library for university, and function for accessing files should be as much complete as possible... Your answer does exactly what I thought, but the question is: are there problems with retrieving external sdcard path in this way? – Massimo Nov 20 '12 at 16:28
  • 1
    The only thing I can think of would be permissions. Would the device need to be rooted to read that file? I don't have a device handy for testing, but when I get home from the office I'll have a play and see. – Rawkode Nov 20 '12 at 16:33
  • Is it too much to ask you to try the piece of code which I've just added into the question? – Massimo Nov 20 '12 at 16:42
0

I use the following code to first detect wether the sdCard exists and then run the relevent code:

Detecting whether SD card exists:

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
    // file path = "/mnt/sdcard/Android/data/PACKAGE_NAME/..."
}
else
{
    // file path = "/data/data/PACKAGE_NAME/..."
}

Think this is what you are after?

SingleWave Games
  • 2,618
  • 9
  • 36
  • 52
-1

This could be the right solution. Read it from /etc/vold.fstab, which lists all the partitions currently mounted on a Linux system (Android included)

String getExternalSdcardDirectory() {
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(new File("/etc/vold.fstab"));
    } catch (FileNotFoundException e) {
        return null;    // should never be reached
    }

    try {
        byte[] buffer = new byte[4096];
        int n=0;

        String file = "";
        while ((n=fis.read(buffer, 0, 4096))>0) {
            file += new String(buffer, 0, n);
        }
        fis.close();

        String[] rows = file.split("\n");
        for (String row: rows) {
            String trimmedRow = row.trim();
            if (trimmedRow.startsWith("#") || trimmedRow.equals(""))
                continue;
            else if (trimmedRow.equals(Environment.getExternalStorageDirectory().getAbsolutePath()))
                continue;
            else
                return trimmedRow.split(" ")[2];

        }
    } catch (IOException e) {
        // nothing
    }
    return null;
}
Massimo
  • 3,436
  • 4
  • 40
  • 68
  • according to this: http://android.stackexchange.com/questions/14530/whats-the-android-equivalent-of-etc-fstab, I have the `/fstab.$systemname.rc` file. – dentex Oct 11 '15 at 14:54
  • Does your device has (or had) a microSD card slot? – Massimo Feb 28 '17 at 17:03
  • Yes, the device had a sdcard slot (it was a galaxy S2). In accordance to the resource above, my device (now a oneplus X) actually have a /fstab.$DEVICE file (here is the /fstab.qcom), but I don't see anything related to sdcards. – dentex Mar 01 '17 at 20:49
  • Correction: there's this: `/devices/msm_sdcc.2/mmc_host* auto auto defaults voldmanaged=sdcard1:auto,encryptable=userdata` – dentex Mar 01 '17 at 20:55