如何在Android级别11中获得SD卡目录?这段代码
Environment.getExternalStorageDirectory();返回电话内存的me目录(内部目录)。我向AndroidManifest.xml添加了仅用于外部存储的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />在一些手机上,这段代码工作正常(例如,中兴通讯,Blade,HN和Phillips),e.t。返回具体的SD卡路径。但联想还会返回内部路径。每部电话都有正式恢复。
发布于 2016-11-05 07:43:27
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;
}最初的方法已经过测试,并与我的手机一起工作。
发布于 2016-11-05 07:45:32
在某些设备中,外部sdcard默认名称显示为extSdCard,而另一些设备则显示为sdcard1。这个代码片段有助于找出确切的路径,并帮助您检索外部设备的路径,就像手机与膝上型计算机连接时一样。
private String[] getPaths()
{
String[] paths = new String[4];
if(new File("/storage/extSdCard/").exists())
paths[0]="/storage/extSdCard/";
if(new File("/storage/sdcard1/").exists())
paths[1]="/storage/sdcard1/";
if(new File("/storage/usbcard1/").exists())
paths[2]="/storage/usbcard1/";
if(new File("/storage/sdcard0/").exists())
paths[3]="/storage/sdcard0/";
return paths;
}https://stackoverflow.com/questions/40435907
复制相似问题