下面是爲獲取sd卡目錄而編寫的代碼。 我一直在使用命令執行並改爲讀取/ proc/mounts。 我的問題是它是否是正確的代碼?在Android設備上獲取sd卡目錄
不是Linux操作系統的專家。所有設備的/ proc/mounts路徑是否相同? 我認爲這段代碼也沒有任何命令注入的可能性。
// Process process = new ProcessBuilder().command("mount").start();
// process.waitFor();
reader = new BufferedReader(new FileReader("/proc/mounts"));
String line;
while ((line = reader.readLine()) != null) {
// Output the line of output from the mount command
logger.debug(" {}", line);
if (line.startsWith("/dev/block/vold/")) {
String[] tokens = line.split(" ");
if (tokens.length >= 3 && (tokens[2].equals("vfat") || tokens[2].equals("exfat"))) {
String path = tokens[1];
File file = new File(path);
if (file.exists() && file.isDirectory()) {
logger.debug("Detected SD card at {}", file.getPath());
if (!file.canWrite()) {
logger.warn("The SD card path {} is reporting that it is not writable", file.getPath());
}
return path;
}
}
}
}
歡呼聲, Saurav
Ÿ這麼多的活動讓Android的SD卡路徑 –
Environment.getExternalStorageDirectory(); –
據我所知,使用這個API將是不正確的,因爲不同的製造商會考慮不同的外部存儲。請查看http://stackoverflow.com/questions/9340332/how-can-i-get-the-list-of -mounted-external-storage-of-android-device/19982338#19982338 – saurav