在Android设备中获取sd卡目录
下面是为检索sd卡目录而编写的代码。 我一直在使用命令的执行,并改为读取/proc/mounts。 我的问题是它是否是正确的代码 不是Linux操作系统专家。所有设备的/proc/mounts路径是否相同? 我认为这段代码也不会有任何命令注入的可能性在Android设备中获取sd卡目录,android,linux,Android,Linux,下面是为检索sd卡目录而编写的代码。 我一直在使用命令的执行,并改为读取/proc/mounts。 我的问题是它是否是正确的代码 不是Linux操作系统专家。所有设备的/proc/mounts路径是否相同? 我认为这段代码也不会有任何命令注入的可能性 // Process process = new ProcessBuilder().command("mount").start(); // process.waitFor(); reader = new Buff
// 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上,您可以通过
Environment.getExternalStorageDirectory();
请使用
Environment.getExternalStorageDirectory()
获取SD卡的路径
另外,对属性
Environment.MEDIA\u MOUNTED
etc使用Environment.getExternalStorageState()
,检查SD卡是否可读、是否已挂载等:)y要获得android SD卡路径环境,需要做很多工作。getExternalStorageDirectory();据我所知,使用这个API将是不正确的,因为不同的手工操作将不同地考虑外部存储。据我所知,使用这个API来检查这一点将是不正确的,因为不同的手工操作将不同地考虑外部存储器。问题也请检查这里。getExternalStorageDirectory()在所有情况下都不起作用,它仍应提供一个可写入的目录。。。我总是用它,从来没有问题。嗯,不要理会我的答案。据我所知,使用这个API将是不正确的,因为不同的手工操作会不同地考虑外部存储器。然而谷歌文档说这是访问它的方法。也在这里查一下。getExternalStorageDirectory()在所有情况下都不起作用