我想从设备读取/写入 OTG USB 存储。为此,我编写了下面的代码,但它仅显示 SD 卡。
HashSet<String> out = new HashSet<String>();
String reg = "(?i).*vold.*(vfat|ntfs|exfat|fat32|ext3|ext4).*rw.*";
String s = "";
try {
final Process process = new ProcessBuilder().command("mount")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
s = s + new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
// parse output
final String[] lines = s.split("\n");
for (String line : lines) {
if (!line.toLowerCase(Locale.US).contains("asec")) {
if (line.matches(reg)) {
String[] parts = line.split(" ");
for (String part : parts) {
if (part.startsWith("/"))
if (!part.toLowerCase(Locale.US).contains("vold"))
out.add(part);
}
}
}
}
for (String s3 : out ) {
Toast.makeText(getApplicationContext(),s3,Toast.LENGTH_LONG).show();
System.out.println("Value: " +s3);
}
使用上面的代码,我可以获得外部 SD 卡,但无法使用 OTG 读取 USB。
我怎样才能获得 USB 存储呢?