如何在Android上获得多个存储文件路径?

时间:2013-05-21 08:36:51

标签: android storage

在我的手机中,我有两个存储空间,其中一个是SD卡,另一个是extsdcard

我可以通过以下方式获取SD卡路径:

Environment.getExternalStorageDirectory()
.toString()

如何获取extsdcard文件路径?

1 个答案:

答案 0 :(得分:2)

使用它来获取其他外部SD卡:

 File storageDir=   new File("/mnt/external_sd/")

OR

 File storageDir=   new File("/mnt/extSdCard/")

有关详细信息,请参阅http://mono-for-android.1047100.n5.nabble.com/detect-SD-Card-path-td5710218.html#a5710250this

因此,像这样使用函数来获取所有分机卡的列表......

public static HashSet<String> getExternalMounts() {
    final HashSet<String> out = new HashSet<String>();
    String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
    String s = "";
    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) {
            s = s + new String(buffer);
        }
        is.close();
    } catch (final Exception e) {
        e.printStackTrace();
    }

    // parse output
    final String[] lines = s.split("\n");
    for (String line : lines) {
        if (!line.toLowerCase(Locale.US).contains("asec")) {
            if (line.matches(reg)) {
                String[] parts = line.split(" ");
                for (String part : parts) {
                    if (part.startsWith("/"))
                        if (!part.toLowerCase(Locale.US).contains("vold"))
                            out.add(part);
                }
            }
        }
    }
    return out;
}