0

My device has two paths which point to two USB ports on the device /storage/udisk/ and /storage/udisk2/ . I would like to find these paths programmatically. I can find via Broadcast Receiver when the devices are inserted but this does not work for devices which are already inserted at boot. Is there a database of devices connected which can be accessed?

Environment.getExternalStorageDirectory() returns the path to the SDCard only and not inserted USB devices.

user2206836
  • 7
  • 1
  • 5
  • `Environment.getExternalStorageDirectory() returns the path to the SDCard`. No. You get a path to external memory. Not to a removable micro sd card. – greenapps Feb 02 '16 at 13:23
  • see here for a solution (my answer was removed): https://stackoverflow.com/questions/27601157/path-of-otg-in-android-device/29413243 – 3c71 Feb 23 '18 at 16:38
  • Possible duplicate of [Path of OTG in Android device](https://stackoverflow.com/questions/27601157/path-of-otg-in-android-device) – 3c71 Feb 23 '18 at 16:56

3 Answers3

0

I am also struggling with the same problem. It seems that different manufacturer have different path for USB.

Mostly the real path for a usb drive is under /mnt/media_rw/ ( e.g. /mnt/media_rw/usbotg), and link path is under /storage/ (for example /storage/usbotg). However, this doesn't happen always. I have tested in 3 different brands and find out the followings:

  • Lenovo Tab2 A10-30 has /storage/usbotg as the link to the USB storage.
  • Nexus 9 shows nothing for usb-relevant path.
  • Samsung mobiles normally name the path /storage/UsbDiskA, /storage/UsbDiskB, ... /storage/UsbDiskF.

When you use ADB shell command "cat /proc/mounts", you can find that some difference can be made after you plug in the USB disk. In my Lenovo tablet, it adds these two new lines:

/dev/block/vold/8:1 /mnt/media_rw/usbotg vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0 /dev/fuse /storage/usbotg fuse rw,nosuid,nodev,noexec,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0

flame3
  • 2,812
  • 1
  • 24
  • 32
  • Have you found a solution using any APIs? On a Nexus 6P, I get a /mnt/media_rw/XXXXX but nothing in /storage/. Hidden API GetVolumeList() doesn't report the USB device! And getExternalFilesDirs() continues to only list the internal storage as if there was no other device! – 3c71 Feb 19 '18 at 15:20
  • see my new answer - libaums – flame3 Mar 01 '18 at 19:38
0

Duplicate of Path of OTG in Android device

The answer shows how to list USB OTG devices and their respective path. And it answers OP's question: "I would like to find these paths programmatically".

I've used the answer's github link to figure out how to find these paths programmatically.

FWIW, all information is gathered from /sys/bus/usb/devices/. The point to filter out non-OTG devices as it is performed in the github library.

3c71
  • 4,313
  • 31
  • 43
0

The open-source library libaums helps a lot when you are working with USB https://github.com/magnusja/libaums

flame3
  • 2,812
  • 1
  • 24
  • 32
  • I want to try this library but i cannot get it to work. Still new to java and how to implement the step properly using libaums. I can import the library using implement step but start with usbmasstorage, my app crash when start. I put it on onCreate() method. I still unable to find the workable way to work with libaums. – Luiey Dec 25 '19 at 14:46
  • I have used it some years ago. So I don't know the compatibility with the current Android libraries. You can check your debug log to see what could be the reason for the crash. – flame3 Jan 17 '20 at 10:48