27

So I have this little cable that you plug into your phone that has a USB port on the other side where you can plug in a flash drive for example, as you can see here:

enter image description here

When I plug in a flash drive I get a notification that says:

USB mass storage connected

When I then launch a file explorer app I can see that the drive is then located at:

/storage/UsbDriveA/

And that's great, but I want to know how to gain access to the flash drive in my code. Getting access to the SD card is easy enough:

File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/MyFiles")
directory.mkdirs();

But how would you do this in the case of the flash drive? Thanks in advance! :)

  • wow what phone that has a USB port?? – DnR Jun 06 '14 at 06:23
  • haha I'll update my question with a pic of the cable I'm using. –  Jun 06 '14 at 06:23
  • Ahh I see. I thought there is a _Female_ USB Port on the phone that can directly plugged without cable -_- – DnR Jun 06 '14 at 06:35
  • 3
    You have to use a file explorer app on your device to find out. Look in /mnt and /storage to find the exact path. – greenapps Jun 06 '14 at 06:35
  • Found that it's located at /storage/UsbDriveA/ . Still in the code what function do you use where you'll post this path as a param? –  Jun 06 '14 at 06:40
  • 3
    Android API guide for [USB Host](http://developer.android.com/guide/topics/connectivity/usb/host.html) should give you all the information that is needed. – Shashank Kadne Jun 06 '14 at 06:44
  • @DeanGrobler, were you able to get this to work? – ChrisVollo Dec 10 '14 at 23:43
  • Possible duplicate of [Android detect usb storage for kitkat (4.4)](http://stackoverflow.com/questions/36208297/android-detect-usb-storage-for-kitkat-4-4). There I posted solutions for pre-6.0 and 6.0/above. – Peter Tran Jun 16 '16 at 22:58

2 Answers2

9

In this example I am using the FileUtils from Apache, but even without it you will see the logic used to read a USB Flash drive:

private UsbManager usbManager;
private UsbDevice clef;
ArrayList<File> images;

usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
clef = null;
    
if (usbManager != null)
{
    HashMap<String,UsbDevice> deviceList = usbManager.getDeviceList();
    if (deviceList != null)
    {
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while (deviceIterator.hasNext()) {
            clef = deviceIterator.next();
        }
    }
}

if (clef != null)
{
    File directory  = new File("/storage/UsbDriveA/");
    if (directory != null) {
        if (directory.canRead()) {
                
            images = new ArrayList<File>();
            String[] imageExtensions = {"jpg","jpeg","png","gif","JPG","JPEG","PNG","GIF"};
            Iterator<File> iterateImages = FileUtils.iterateFiles(directory, imageExtensions, true);
            while (iterateImages.hasNext()) {
                File theImage = iterateImages.next();
                if (!theImage.getName().startsWith(".", 0))
                    images.add(theImage);
            }
            
            // custom process / methods... not very relevant here : 
            imageIndex = 0;
            scale = 1.0f;
            countImgs = images.size();
            loadImage(imageIndex);
        }
    }
}

In my manifest I have those lines, although I'm not sure they're all mandatory...

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
jox
  • 2,218
  • 22
  • 32
JBA
  • 2,889
  • 1
  • 21
  • 38
  • I can see that some are saying their usb flash drive is accessible thru usbdisk path and not UsbDriveA : if you want to cover all cases you can test both : if with /storage/UsbDriveA/ your usbManager is still null, then try with /storage/usbdisk/ etc... (and maybe a few others if you can find a list of different paths, although there shouldn't be a huge number of different paths here) – JBA Feb 23 '15 at 07:28
3

I can't test this, not having such a cable myself, but my assumption would be that you can pass your filepath directly into the constructor which would look like:

File directory  = new File("/storage/UsbDriveA/");

Have you tried this?

ballesta25
  • 93
  • 1
  • 5