這可能是正確的解決方案。從/etc/vold.fstab,其中列出了當前安裝在Linux系統上的所有分區閱讀(安卓含稅)
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;
}
Environment.getExternalStorageDirectory()是SD卡 – njzk2
請記住您的解決方案可能無法正常工作在Android 4.2上,給定多個用戶帳戶功能。目前還不清楚這些卷一般可以在多大程度上訪問,更不用說4.2設備在被次級用戶使用時如何處理它們。 – CommonsWare
爲什麼它不應該工作? (想要了解以便找到可能的解決方案) – Massimo