user1204624
user1204624

Reputation: 11

Is there a way to determine whether a specified dir is in removable sdcard?

On some phones, there are two external storage directories. For example, on some phones, there are /sdcard(internal storage) and /sdcard/external_sd (removable stick or others). These two directores may differ on different phones, for example, there may be /sdcard and /sdcard/_External_SD.

My question is, for a given directory, is there any way to determine where it is mounted? i.e., how to find whether this dir is mounted on /sdcard or /sdcard/external_sd. regex shouldn't work well as the directories names are different on different phones. Using this code:

Environment.getExternalStorageDirectory()

Just returns the "primary" external storage directory, so it doesn't seem to help either.

Upvotes: 1

Views: 144

Answers (1)

fardjad
fardjad

Reputation: 20394

Maybe the mount command output helps.

The following gives you (the first) alternate external storage path:

String getAlternateExternalStoragePath() {
    try {
        Process process = Runtime.getRuntime().exec("mount");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        int read;
        char[] buffer = new char[1024];
        StringBuffer output = new StringBuffer();
        while ((read = reader.read(buffer)) > 0) {
            output.append(buffer, 0, read);
        }
        reader.close();    
        process.waitFor();

        String sdcardPath = android.os.Environment.getExternalStorageDirectory().toString() + "/";
        Pattern pattern = Pattern.compile(".+ on (" + sdcardPath + ".+) type vfat");
        Matcher matcher = pattern.matcher(output.toString());

        if (matcher.find()) return matcher.group(1);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

// ...

String alternateExternalStoragePath = getAlternateExternalStoragePath();

Upvotes: 1

Related Questions