Android list files from USB Drive

倖福魔咒の 提交于 2019-12-05 07:24:52

问题


I currently have a working implementation that allows me to list USB devices, request permission for that USB device and then connect to them (mainly from http://developer.android.com/guide/topics/connectivity/usb/host.html). The code I have is the same as the linked article but none of the methods on the classes provided are for listing files on the device or checking if files exist.

However I do not understand how I can list files that are on the USB device to check if files exists before transferring data.

The device "path" is along the lines of /dev/bus/usb/00x but this can't be accessed directly through File. I see the app ES File Explorer is able to list files on a USB (non-rooted device) so I know it is possible. I have also tried accessing URI paths like usb://100x as it seemed like ES File Explorer was doing that, but I had no luck.

I have tried using the Storage Access Framework. On a Samsung Galaxy Tab it works with both SD Cards and USBs, however on the Google Nexus 10 it does not work with either (it has no SD Card slot) so I'm trying to implement another way to talk to the USB.

Version is Lollipop and above.

Any ideas? Thanks.


回答1:


Please refer this link. Every mass storage device has at least one interface descriptor with the class code 08h, which stands for the mass storage class. The mass storage class is not defined in the device descriptor! The USB interface has exactly two endpoint descriptors. One IN endpoint to read from the device and one OUT endpoint to write to the device2. Reading and writing in this case does not necessarily mean reading or writing on the actual storage medium, this is described later. There are two different types regarding the mass storage class. There is the bulk-only transport (BBB) mechanism which is the most common one. All newer devices follow that standard. Then there is the Control/Bulk/Interrupt (CBI) standard which is no longer important, because the USB-IF recommends using the BBB approach

UsbDevice is recognized as massStorage Device If:

usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_MASS_STORAGE
                        || usbInterface.getInterfaceSubclass() == INTERFACE_SUBCLASS // int 6
                        || usbInterface.getInterfaceProtocol() == INTERFACE_PROTOCOL // int 80

and

usbInterface.getEndpointCount() == 2

where one of endpoint must satisfy following:

endPoint direction == 0
endPoint type = UsbConstants.USB_ENDPOINT_XFER_BULK //int 2


来源:https://stackoverflow.com/questions/34795622/android-list-files-from-usb-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!