【发布时间】:2014-02-18 12:19:56
【问题描述】:
以下是为检索 sd 卡目录而编写的代码。 我一直在使用执行命令并更改为读取 /proc/mounts。 我的问题是它是否是正确的代码?
不是 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;
}
}
}
}
干杯, 索拉夫
【问题讨论】:
-
要获取 android sdcard 路径需要做很多事情
-
Environment.getExternalStorageDirectory();
-
据我所知,使用这个 API 是不正确的,因为不同的制造商会以不同的方式考虑外部存储。检查这个stackoverflow.com/questions/9340332/…