gpt4 book ai didi

android - Environment.getExternalStorageDirectory().getAbsolutePath() 返回 sdcard0 但我的 SD 在 sdcard1 中

转载 作者:行者123 更新时间:2023-11-30 03:18:39 25 4
gpt4 key购买 nike

我尝试使用 Environment.getExternalStorageDirectory().getAbsolutePath() 将一些东西写入 SD 卡手机内存以获取 SD 路径,结果是/storage/sdcard0。但是我的SD卡是/storage/sdcard1。如何获取真实的SD卡路径?

我可以在“/”中看到有一个名为/ext_sd 的符号链接(symbolic link)链接到/storage/sdcard1。此符号链接(symbolic link)在所有设备中?

最佳答案

我在这篇文章中找到了解决方案:How can I get external SD card path for Android 4.0+?

/**
* Returns the list of external devices mounted.
*
* @return HashSet<String>.
*/
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;
}

关于android - Environment.getExternalStorageDirectory().getAbsolutePath() 返回 sdcard0 但我的 SD 在 sdcard1 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com