Get access to USB mass storage device in android

user818700

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! :)

JBA

In this example I am using the FileUtils from Apache, but event 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" />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to access usb mass storage in android NDK

From Dev

How to get the file path from a device acting as a usb mass storage in android

From Dev

Block USB access, but allow only specific ones (in particular, one USB mass storage device)

From Dev

Read bytes of a USB mass storage device

From Dev

Find out if a specific device is an USB mass storage

From Dev

USB mass storage device split to 2 devices

From Dev

USB mass storage couldn't get mounted

From Dev

How to automount an Android phone as USB mass storage?

From Dev

Access and manipulate files and directories on an external USB storage in a rooted Android device

From Dev

How to install CentOS 6 via USB mass storage device?

From Dev

Finding Volume Label of a usb mass storage device using python

From Dev

How to install CentOS 6 via USB mass storage device?

From Dev

How to share a folder as an USB mass storage device (from a raspberry pi)

From Dev

How to auto-sync with a plugged-in USB mass storage device?

From Dev

Finding Volume Label of a usb mass storage device using python

From Dev

file is too large to copy on 8 giga USB Mass Storage Device

From Dev

The via USB connected SCSI (UAS)-device for mass storage cannot be stopped

From Dev

Disable read cache/buffer for USB mass storage device in Linux

From Dev

Disabling read buffering in Linux for USB mass storage device

From Dev

Disable usb mass storage

From Dev

Disable usb mass storage

From Dev

Mounting USB mass storage devices without root access

From Dev

Access USB device storage via terminal

From Dev

disable MTP udev rules for specific device so it can be mount as a USB Mass Storage device

From Dev

Linux USB Mass Storage Emulation

From Dev

Can I make a USB port on my Linux computer look like a mass storage device?

From Dev

How to read from usb mass storage device when AppSand box is enabled

From Dev

Can I make a USB port on my Linux computer look like a mass storage device?

From Dev

How can I remove all drivers and other files related to a USB Mass Storage device?

Related Related

  1. 1

    How to access usb mass storage in android NDK

  2. 2

    How to get the file path from a device acting as a usb mass storage in android

  3. 3

    Block USB access, but allow only specific ones (in particular, one USB mass storage device)

  4. 4

    Read bytes of a USB mass storage device

  5. 5

    Find out if a specific device is an USB mass storage

  6. 6

    USB mass storage device split to 2 devices

  7. 7

    USB mass storage couldn't get mounted

  8. 8

    How to automount an Android phone as USB mass storage?

  9. 9

    Access and manipulate files and directories on an external USB storage in a rooted Android device

  10. 10

    How to install CentOS 6 via USB mass storage device?

  11. 11

    Finding Volume Label of a usb mass storage device using python

  12. 12

    How to install CentOS 6 via USB mass storage device?

  13. 13

    How to share a folder as an USB mass storage device (from a raspberry pi)

  14. 14

    How to auto-sync with a plugged-in USB mass storage device?

  15. 15

    Finding Volume Label of a usb mass storage device using python

  16. 16

    file is too large to copy on 8 giga USB Mass Storage Device

  17. 17

    The via USB connected SCSI (UAS)-device for mass storage cannot be stopped

  18. 18

    Disable read cache/buffer for USB mass storage device in Linux

  19. 19

    Disabling read buffering in Linux for USB mass storage device

  20. 20

    Disable usb mass storage

  21. 21

    Disable usb mass storage

  22. 22

    Mounting USB mass storage devices without root access

  23. 23

    Access USB device storage via terminal

  24. 24

    disable MTP udev rules for specific device so it can be mount as a USB Mass Storage device

  25. 25

    Linux USB Mass Storage Emulation

  26. 26

    Can I make a USB port on my Linux computer look like a mass storage device?

  27. 27

    How to read from usb mass storage device when AppSand box is enabled

  28. 28

    Can I make a USB port on my Linux computer look like a mass storage device?

  29. 29

    How can I remove all drivers and other files related to a USB Mass Storage device?

HotTag

Archive