2

我需要 SdCard 路径来保存文件。我尝试了一些代码,但这些代码在某些设备或 Android 版本上不起作用。现在我需要一个代码/路径来获取所有设备和所有 Android 版本的 SdCard 路径。

4

3 回答 3

1

要获取 sdcard 路径,请尝试以下代码:

public static String getExternalSDCardRootDirectory() {


    String cmdMOUNT = "cat /proc/mounts";
    Runtime run = Runtime.getRuntime();
    List<String> paths = new ArrayList<>();
    try {
        Process p = run.exec(cmdMOUNT);
        BufferedInputStream in = new BufferedInputStream(p.getInputStream());
        BufferedReader inBr = new BufferedReader(new InputStreamReader(in));

        String lineStr;
        while ((lineStr = inBr.readLine()) != null) {
            Log.d(TAG, lineStr);
            if (lineStr.toLowerCase().contains("sdcard") || lineStr.toLowerCase().contains("ext") ) {
                String[] strArray = lineStr.split(" ");
                if (strArray.length >= 3 &&
                        (!strArray[1].contains("/system") && 
                        !strArray[1].contains("/data") &&
                        !strArray[1].contains("/cache") &&
                        !strArray[1].contains("/persist") 
                        )) {

                    String result = strArray[1].trim();

                    if((result.contains("ext") || result.contains("1")) && result.contains("storage")) {
                        paths.add(result);
                    }
                    //return result;
                }
            }
            if (p.waitFor() != 0 && p.exitValue() == 1) {
                Log.e(TAG, "check mount info failed");
                return null;
            }
        }
        inBr.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    if (paths.size() > 0) {
        return paths.get(0);
    }
    else {
        return null;
    }
}
于 2018-11-01T01:23:53.087 回答
0

我找到了一个现有的帖子

只需将其更改为...

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;
}

在不同平台上测试的解决方案可以在这里找到。

于 2018-11-01T01:47:04.937 回答
0

要获取路径,您需要调用Environment.getExternalStorageState()

于 2018-10-31T23:37:30.227 回答