简体   繁体   English

如何读取OTG USB存储

[英]How to read OTG USB storage

I want to read/write OTG USB storage from device.我想从设备读取/写入 OTG USB 存储设备。 For that I have written the code below but it shows only the SD card.为此,我编写了下面的代码,但它只显示了 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);
}

With the code above I am able to get external SD card but not able to read USB with OTG.使用上面的代码,我可以获得外部 SD 卡,但无法使用 OTG 读取 USB。

How can I get USB storage also?我怎样才能获得 USB 存储设备?

I've looked at your other questions regarding OTG access, and I think you're making it hard on yourself.我已经查看了您关于 OTG 访问的其他问题,我认为您对自己太苛刻了。 The easiest way IMO to access/read/write OTG on a very wide range of devices is by querying all these directories, and using shell to read/write to them: IMO 在非常广泛的设备上访问/读取/写入 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)

Tested on my collection of devices (and a few others'), and a quick trip to Bestbuy for testing the latest flagships.在我收集的设备(和其他一些设备)上进行了测试,并快速前往 Bestbuy 测试最新的旗舰产品。 The only popular devices I know I'm missing are Hawuei/Xioami devices based in China, which are becoming popular in English countries.我知道我缺少的唯一流行设备是位于中国的 Hawuei/小米设备,它们在英国国家/地区越来越流行。

You would be using Runtime.exec() or ProcessBuilder and testing the length of the ls command output on each directory (to find the mounted USB directory), and cp or redirection > to write files to the pendrive, and cat to read off the pendrive.您将使用Runtime.exec()ProcessBuilder并测试每个目录上ls命令输出的长度(以查找安装的 USB 目录),并使用cp或重定向>将文件写入随身碟,并使用cat读取随身碟。

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM