如何在Android中获取SdCard路径?

时间:2018-10-31 23:32:07

标签: java android kotlin android-storage

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

3 个答案:

答案 0 :(得分:1)

要获取路径,您需要致电summarise_at

答案 1 :(得分:0)

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

def grabPks(pageNum):
    # cloudflare blocks bots...use scraper library to get around this or build your own logic to store and use a manually generated cloudflare session cookie... I don't care 

    req = scraper.get("sumurl.com/"+str(pageNum)).content
    if(req == b'Rate Limit Exceeded'):
        print("adjust the rate limiting because they're blocking us :(")
        manPenalty = napLength * 3
        print("manually sleeping for {} seconds".format(manPenalty))
        time.sleep(manPenalty)
        print("okay let's try again... NOW SERVING {}".format(pageNum))
        return grabPks(pageNum)
    else:
        tree = html.fromstring(req)
        pk = tree.xpath("/path/small/text()")
        resCmpress = tree.xpath("path/a//text()")
        resXtend = tree.xpath("[path/td[2]/small/a//text()")
        balance = tree.xpath("path/font//text()")
        return pk, resCmpress, resXtend, balance

答案 2 :(得分:0)

我找到了existing post

只需将其更改为...

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

可以在here上找到在不同平台上经过测试的解决方案。