I am writing an application where Android device is the Host. User will connect an USB drive to the Android device and my application will write some text files in the USB drive. The path of the text files will be like USB_DRIVE/Data/APP_NAME/myfile.txt
inside the USB drive.
Application should also be able read the text files from the particular location or even better if it could list all the files in USB_DRIVE/Data/APP_NAME
folder and User can choose a file.
I have found the code in official android dev site
UsbInterface intf = device.getInterface(0);
UsbEndpoint endpoint = intf.getEndpoint(0);
UsbDeviceConnection connection = mUsbManager.openDevice(device);
connection.claimInterface(intf, forceClaim);
connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT);
I am not sure how to write a file in a specific path of the USB_DRIVE using bytes array.
FYI: there is no path in my device similar to /storage/usb0/
or /storage/usbdisk0/
.
Inside /storage/
, the file structure is like following,
- storage
- emulated
- 0
- SD_CARD content
- legacy
- SD_CARD content
- 0
- sdcard0 - SD_CARD content
- emulated
My test device is Nexus 10.
I tried with ES File explorer app and using this app I can successfully move/copy any files from SD_CARD to USB_DRIVE; i.e: OTG is working fine and read/write files from App to USB_DRIVE is doable. After transferring any files, ES shows the path of the file like, usb://1002/myfile.txt
.
If I could write/move/read files using ES intent or something that will work too.
or, If any FTP solution can help me to achieve the functionality, I will go for it.
Thank you.