gpt4 book ai didi

android - 如何获取 Android 4.0+ 的外部 SD 卡路径?

转载 作者:IT老高 更新时间:2023-10-28 13:06:46 28 4
gpt4 key购买 nike

Samsung Galaxy S3有一个外置SD卡槽,安装在/mnt/extSdCard上。

如何通过 Environment.getExternalStorageDirectory() 之类的方式获取此路径?

这将返回 mnt/sdcard,我找不到外部 SD 卡的 API。 (或某些平板电脑上的可移动 USB 存储设备。)

最佳答案

我找到了一个解决方案的变体 here

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

原始方法经过测试和使用

  • 华为 X3(现货)
  • Galaxy S2(现货)
  • Galaxy S3(现货)

我不确定它们在测试时使用的是哪个 android 版本。

我已经用

测试了我的修改版本
  • Moto Xoom 4.1.2(库存)
  • 使用 otg 电缆的 Galaxy Nexus (cyanogenmod 10)
  • HTC Incredible (cyanogenmod 7.2) 这返回了内部和外部。这个设备有点奇怪,因为 getExternalStorage() 返回的是 sdcard 的路径,所以它的内部大部分都没有使用。

以及一些使用 sdcard 作为主存储的单一存储设备

  • HTC G1 (cyanogenmod 6.1)
  • HTC G1(现货)
  • HTC Vision/G2(库存)

除了 Incredible 之外,所有这些设备都只返回了它们的可移动存储。我可能应该做一些额外的检查,但这至少比我迄今为止找到的任何解决方案都要好一些。

关于android - 如何获取 Android 4.0+ 的外部 SD 卡路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281010/

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