本文介绍了如何读取OTG USB存储器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从设备读取/写入OTG USB存储.为此,我编写了以下代码,但仅显示了SD卡.

I want to read/write OTG USB storage from device. For that I have written the code below but it shows only the SD card.

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.

With the code above I am able to get external SD card but not able to read USB with OTG.

我还如何获得USB存储设备?

How can I get USB storage also?

推荐答案

我查看了您有关OTG访问的其他问题,我认为您对此感到很费力. IMO在多种设备上访问/读取/写入OTG的最简单方法是查询所有这些目录,并使用Shell对其进行读取/写入:

I've looked at your other questions regarding OTG access, and I think you're making it hard on yourself. 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:

/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设备,这些设备在英国国家/地区变得越来越流行.

Tested on my collection of devices (and a few others'), and a quick trip to Bestbuy for testing the latest flagships. The only popular devices I know I'm missing are Hawuei/Xioami devices based in China, which are becoming popular in English countries.

您将使用Runtime.exec()ProcessBuilder并在每个目录上测试ls命令输出的长度(以查找已安装的USB目录),并使用cp或重定向>来将文件写入到笔驱动器,然后按cat读取笔驱动器.

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.

希望这会有所帮助

这篇关于如何读取OTG USB存储器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:48