问题描述
我面对获取外部SD卡的路径的众所周知的问题装在某些Android设备。 (见this理解我的意思)的问题
I'm faced with the well-known problem of obtaining the path of an external SD card mounted on some Android devices. (see this question for understanding what I mean)
我想过通过阅读 /etc/vold.fstab
,然后取刚刚重新线presenting分区的内容来解决这个问题,但我不'T有这样做的测试设备。
我想要做的就是读取该文件,忽略它是指由 Environment.getExternalStorageDirectory()
返回的地址的行,并采取其它行(如present)。
I've thought to solve the problem by reading the content of /etc/vold.fstab
, then taking just lines representing partitions, but I don't have a device for doing tests.What I want to do is to read that file, ignore the row which refers to the address returned by Environment.getExternalStorageDirectory()
, and take the other row (if present).
我不知道什么(我没有测试它的可能性)是:有没有在其中,我可以有其他线路不属于外部SD卡的情况下? SD卡,如果present,出现在文件vold.fstab?
What I don't know (and I don't have the possibility to test it) is: are there cases in which I can have other lines which are not the external SD card? The SD card, if present, appears on the file vold.fstab?
修改:
答案是:YES。阅读接受的答案。
edit:The answer is: YES. Read the accepted answer.
推荐答案
这可能是正确的解决方案。从/etc/vold.fstab,其中列出了当前安装在Linux系统上(包括Android版)
This could be the right solution. Read it from /etc/vold.fstab, which lists all the partitions currently mounted on a Linux system (Android included)
String getExternalSdcardDirectory() {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("/etc/vold.fstab"));
} catch (FileNotFoundException e) {
return null; // should never be reached
}
try {
byte[] buffer = new byte[4096];
int n=0;
String file = "";
while ((n=fis.read(buffer, 0, 4096))>0) {
file += new String(buffer, 0, n);
}
fis.close();
String[] rows = file.split("\n");
for (String row: rows) {
String trimmedRow = row.trim();
if (trimmedRow.startsWith("#") || trimmedRow.equals(""))
continue;
else if (trimmedRow.equals(Environment.getExternalStorageDirectory().getAbsolutePath()))
continue;
else
return trimmedRow.split(" ")[2];
}
} catch (IOException e) {
// nothing
}
return null;
}
这篇关于我怎样才能正确地获取外部SD卡路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!