【问题标题】:How to list files on USB OTG device如何列出 USB OTG 设备上的文件
【发布时间】:2016-01-29 16:34:24
【问题描述】:

我知道该怎么做:

  • 监听 USB 设备的附加和分离事件
  • 获取所有连接的设备
  • 获取设备权限

所以最后我有一个UsbDevice 并且我有权读取/写入它。从这里开始怎么走?

我可以打开设备并获得FileDescriptor,如下所示:

 UsbManager manager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
 UsbInterface intf = device.getInterface(0);
 UsbEndpoint endpoint = intf.getEndpoint(0);
 UsbDeviceConnection connection = manager.openDevice(device);
 boolean interfaceClaimed = connection.claimInterface(intf, true);
 int fileDescriptor = connection.getFileDescriptor();

我现在如何处理这个FileDescriptor?或者我还能如何访问 USB 设备上的文件?

编辑

  • android 解决方案:不要打开UsbDeviceConnection,而只是尝试查找USB 设备路径。尽管如此,你还是有区分多个usb设备的问题,不知道哪个路径属于哪个设备...

  • android >= 6的解决方案:使用存储访问框架,实际上我还不知道它是如何工作的......

【问题讨论】:

  • 大多数情况下,存储棒将安装在文件系统文件夹上,例如 /storage/usbdrive 左右。在 Marshmellow 下,该路径不存在,您可以使用存储访问框架。
  • 我编辑了我的问题并添加了一些内容。其实你知道如何区分USB设备吗?我可以找到所有可用的 USB 存储路径,但我不知道哪个路径属于哪个设备
  • 从未见过超过一个 USB 驱动器路径。您在哪里看到的更多?
  • 通过usb otg你可以安装任何东西,USB驱动笔,外置硬盘甚至是外置读卡器,最终里面有很多卡......
  • 是的。当里面有很多卡片时,你会看到什么路径?哪个设备显示?

标签: android usb usb-otg


【解决方案1】:

因为我和你一样难过,所以我会走使用ls 命令的路线。

代码示例:

try {
    Process process = Runtime.getRuntime().exec("ls /storage/UsbDriveA");
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String listOfFiles = "";
    String line;
    while ((line = buffer.readLine()) != null) {
      listOfFiles += line;
    }
} catch (InterruptedException e) {
    e.printStackTrace();
}

如果挂载点不正确,ls 将返回 no such file or directory

以下是制造商使用的许多安装点的列表:

/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)

(基于我收集的设备和其他 3 个人的设备,以及快速前往 Bestbuy 测试最新的旗舰产品。我知道我缺少的唯一流行设备是位于中国的 Hawuei/Xioami 设备,它们正变得越来越流行在英语国家/地区,但他们可能使用上述之一。)

要找到正确的挂载点,您需要监听 no such file or directory 并使用下一个挂载点循环/重新执行 exec() 语句。

完成所有操作后,您可以使用cat /storage/.../file.txt 轻松读取文件并使用重定向进行写入:echo test > /storage/.../file.txt

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多