Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list files on USB OTG device

I know how to do following:

  • listen to attach and detach events of usb devices
  • getting all attached devices
  • getting permissions for a device

So in the end I have an UsbDevice and I have permissions to read/write it. How to go on from here?

I can open the device and get a FileDescriptor like following:

 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();

How can I work with this FileDescriptor now? Or how else can I access the files on the USB device?

EDIT

  • Solution for android < 6: Don't open a UsbDeviceConnection and just try to find the usb devices path. Still, then you have the problem to distinguish between multiple usb devices and don't know which path belongs to which device...

  • Solution for android >= 6: Use the Storage Access Framework, actually I don't know how this works yet...

like image 361
prom85 Avatar asked Oct 30 '22 10:10

prom85


1 Answers

Because I am as stumped as you are, I would go the route of using the ls command.

Code example:

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 will return no such file or directory if the mount point is incorrect.

Here is a list of many of the mount points manufacturers use:

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

(Based on my collection of devices and 3 other people's, 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, but likely they use one of the above.)

To find the right mount point, you would listen for no such file or directory and loop/re-execute the exec() statement with the next mount point.

Once all that is done, you can easily read files by using cat /storage/.../file.txt and write by using redirection: echo test > /storage/.../file.txt

Hope this helps

like image 98
Aaron Gillion Avatar answered Nov 15 '22 06:11

Aaron Gillion