Class UsbMassStorageDevice


  • public class UsbMassStorageDevice
    extends java.lang.Object
    Class representing a connected USB mass storage device. You can enumerate through all connected mass storage devices via getMassStorageDevices(Context). This method only returns supported devices or if no device is connected an empty array.

    After choosing a device you have to get the permission for the underlying UsbDevice. The underlying UsbDevice can be accessed via getUsbDevice().

    After that you need to call setupDevice(). This will initialize the mass storage device and read the partitions ( Partition).

    The supported partitions can then be accessed via getPartitions() and you can begin to read directories and files.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Releases the UsbInterface and closes the UsbDeviceConnection.
      BlockDeviceDriver getBlockDevice()
      Returns the block device interface for this device.
      static UsbMassStorageDevice[] getMassStorageDevices​(android.content.Context context)
      This method iterates through all connected USB devices and searches for mass storage devices.
      java.util.List<Partition> getPartitions()
      Returns the available partitions of the mass storage device.
      android.hardware.usb.UsbDevice getUsbDevice()
      This returns the UsbDevice which can be used to request permission for communication.
      void init()
      Initializes the mass storage device and determines different things like for example the MBR or the file systems for the different partitions.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getMassStorageDevices

        public static UsbMassStorageDevice[] getMassStorageDevices​(android.content.Context context)
        This method iterates through all connected USB devices and searches for mass storage devices.
        Parameters:
        context - Context to get the UsbManager
        Returns:
        An array of suitable mass storage devices or an empty array if none could be found.
      • init

        public void init()
                  throws java.io.IOException
        Initializes the mass storage device and determines different things like for example the MBR or the file systems for the different partitions.
        Throws:
        java.io.IOException - If reading from the physical device fails.
        java.lang.IllegalStateException - If permission to communicate with the underlying UsbDevice is missing.
        See Also:
        getUsbDevice()
      • close

        public void close()
        Releases the UsbInterface and closes the UsbDeviceConnection. After calling this method no further communication is possible. That means you can not read or write from or to the partitions returned by getPartitions().
      • getPartitions

        public java.util.List<Partition> getPartitions()
        Returns the available partitions of the mass storage device. You have to call init() before calling this method!
        Returns:
        List of partitions.
      • getUsbDevice

        public android.hardware.usb.UsbDevice getUsbDevice()
        This returns the UsbDevice which can be used to request permission for communication.
        Returns:
        Underlying UsbDevice used for communication.
      • getBlockDevice

        public BlockDeviceDriver getBlockDevice()
        Returns the block device interface for this device. Only use this if you know what you are doing, for a interacting (listing/reading/writing files) with a pen drive this is usually not needed
        Returns:
        The BlockDeviceDriver implementation