11

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
    • sdcard0 - SD_CARD content

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.

tausiq
  • 937
  • 1
  • 13
  • 23
  • `I can successfully move/copy any files from SD_CARD to USB_DRIVE;`. Please mention exact paths. Not abbreviations. – greenapps Oct 21 '14 at 11:07
  • If there is no path like `/storage/usbdisk0/` on your device for usbotg then how you you reach the usb stick with ES File Explorer? – greenapps Oct 21 '14 at 11:11
  • Have you looked under `/mnt` ? – greenapps Oct 21 '14 at 11:14
  • While a few vendors do, stock android simply does not mount usb volumes, so there will not be a path to the contents. You have to do things the hard way by implementing mass storage and filesystem code within your app. This has been covered here many times before. – Chris Stratton Oct 21 '14 at 13:27
  • @ChrisStratton please could you provide some links as I've searched SO and haven't found anything obviously relevant (could be I don't know what to search for / how to find it, of course). Here's our bug report (for the Kiwix opensource app) https://sourceforge.net/p/kiwix/bugs/757/ Thanks Julian – JulianHarty Oct 22 '14 at 13:42
  • As I said, there is no built in support for this. You have to build it yourself. Requests for code are considered off-topic on stack overflow. – Chris Stratton Oct 22 '14 at 14:17
  • I'm looking for a way to do I/O (eg, copy files to and from) a usb mass storage device connected via usb otg cable. Have you had any success? @ChrisStratton, I've seen your name on quite a few questions pertaining to this topic, have you seen any solutions for easy i/o with the usb mass storage via otg? – ChrisVollo Dec 23 '14 at 17:57
  • As I have said repeatedly, there is no built in support for this - it is a very involved operation for which you have to provide the code yourself. – Chris Stratton Dec 24 '14 at 10:25
  • 1
    ES can do read/write operation from and to USB_DRIVE. After transferring a file to USB using ES, the path is something like ```usb://1002/myfile.txt```. My best guesss is, they are using some protocol, thats why the double forward slash. Are there any usb:// protocol you guys known of or came across? – tausiq Dec 24 '14 at 19:00
  • ES can do this because they implement everything required above the USB level **within the app itself**. If you want to do something similar, you will have to as well. A request for specific code implementing this would be off topic per SO policy on questions that request external resources or libraries. – Chris Stratton May 25 '16 at 20:41

0 Answers0