我想从设备读取/写入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存储?
发布于 2016-03-23 12:26:25
我已经看过您关于OTG访问的其他问题,我认为您正在使自己变得困难。在非常广泛的设备上访问/读/写OTG的最简单方法是查询所有这些目录,并使用shell对其进行读/写:
/storage/UsbDriveA (all Samsung devices)
/storage/USBstorage1 (LG G4, V10, G3, G2, other LG devices)
/storage/usbdisk (Moto Maxx, Turbo 2, Moto X Pure, other Motorola devices)
/storage/usbotg (Sony Xperia devices, Lenovo Tabs)
/storage/UDiskA (Oppo devices)
/storage/usb-storage (Acer Iconia Tabs)
/storage/usbcard (Dell Venue -- Vanilla Android 4.3 tablet)
/storage/usb (HTC One M7, and some Vanilla Android devices)在我收集的设备(和其他一些设备)上进行了测试,并快速访问Bestbuy以测试最新的旗舰产品。我知道我唯一怀念的流行设备是总部设在中国的Hawuei/Xioami设备,它们在英语国家变得流行起来。
您将使用Runtime.exec()或ProcessBuilder,并在每个目录上测试ls命令输出的长度(以找到挂载的USB目录),并使用cp或重定向>将文件写入挂载驱动器,使用cat读取挂载驱动器。
希望这能有所帮助
https://stackoverflow.com/questions/36060221
复制相似问题