Android USB OTG: How to detect Mass Storage hot-plug event, then copy data?

2.2k views Asked by At

I have an Android-based Huawei tablet, running Lollipop, and want to know how to detect when USB Mass Storage OTG has been hotplugged, and how to copy files from the OTG to tablet internal storage once the device is connected.

I haven't been able to find a workable solution after some searching. For example, the following StackOverflow answers didn't provide a workable solution for my case. Just wondering if anyone can suggest a document, project or sample code to look at to solve the problem, or could point me the APIs I'd need to use?

Transfer Files USB Mass Storage OTG

How app can access files on USB OTG storages in Android 6.0 (API level 23) without root?

1

There are 1 answers

0
Shawney On

To detect a USB being mounted/unmounted you can use UsbHost

You basically have 2 options to read/write files from USB device.

Storage Access Framework - this will not work on all devices. It depends on the permission set up by the manufacturer of the device. From my experience it works on most Samsung devices, but not on HTC or Nexus. I'm not sure about Huawei.

UsbDeviceConnection - This is pretty low level stuff, but there is a library you can use with good examples: https://github.com/magnusja/libaums

There is also a pretty good open source file manager you should check out: https://github.com/1hakr/AnExplorer