Y
Y
YoungSkipper2014-01-15 14:43:27
Android
YoungSkipper, 2014-01-15 14:43:27

How to programmatically get the path to the physical SD card on Android devices?

For example, on Samsung Galaxy s2 / s3, if you use getExternalFilesDir, then we get a path like /storage/sdcard0/Android/data/com.company.app/files/ when the real physical SD card is on /mnt/extSdcard, and /storage/sdcard0 mapped to internal memory
On some devices getExternalFilesDir returns the correct path, but on many the behavior is the same as above.
Share the code or idea how to get the path to at least one of the physically installed SD cards.
android 2.3.3+

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@ntkt, 2014-01-15
_

Android simply doesn't have such a thing as a "real physical SD card".
All you can do is iterate through all the mount points from various sources (output of a mount or /proc/mounts command, vold.fstab file in /system/etc, etc.) and test them with various guesses like:
-- pre-known paths (such as your /mnt/extSdcard)
-- the path contains a known string
-- ...
Everyone suffers somehow.

E
Evgenij_Popovich, 2014-01-17
@Evgenij_Popovich

I am using the mount command. Solution found on stackoverflow

/**
     * Returns possible external sd card path. Solution taken from here
     * http://stackoverflow.com/a/13648873/527759
     * 
     * @return
     */
    public static Set<String> getExternalMounts() {
        final Set<String> out = new HashSet<String>();
        try {
            String reg = ".*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
            StringBuilder sb = new StringBuilder();
            try {
                final Process process = new ProcessBuilder().command("mount")
                        .redirectErrorStream(true).start();
                process.waitFor();
                final InputStream is = process.getInputStream();
                final byte[] buffer = new byte[1024];
                while (is.read(buffer) != -1) {
                    sb.append(new String(buffer));
                }
                is.close();
            } catch (final Exception e) {
                e.printStackTrace();
            }
            // parse output
            final String[] lines = sb.toString().split("\n");
            for (String line : lines) {
                if (!line.toLowerCase(Locale.ENGLISH).contains("asec")) {
                    if (line.matches(reg)) {
                        String[] parts = line.split(" ");
                        for (String part : parts) {
                            if (part.startsWith("/"))
                                if (!part.toLowerCase(Locale.ENGLISH).contains("vold")) {
                                    CommonUtils.debug(TAG, "Found path: " + part);
                                    out.add(part);
                                }
                        }
                    }
                }
            }
        } catch (Exception ex) {
            error(TAG, ex);
        }
        return out;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question