SoFunction
Updated on 2025-04-09

How to read USB disk through android

This article shares the specific code of Android reading USB through USB for your reference. The specific content is as follows

1. Related compile ‘:libaums:+’

2. Permission settings

<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name=".READ_EXTERNAL_STORAGE" />
<uses-permission android:name="" android:required="false" /> 
<uses-feature android:name="" android:required="true" />

3. Listen to the U disk, insert and unplugged broadcast

 // Listen to otg insertion and unplug IntentFilter usbDeviceStateFilter = new IntentFilter();
 (UsbManager.ACTION_USB_DEVICE_ATTACHED);
 (UsbManager.ACTION_USB_DEVICE_DETACHED);
 (UDiskMountedReceiver, usbDeviceStateFilter);
 //Register and listen to custom broadcast IntentFilter filter = new IntentFilter(Constant.ACTION_USB_PERMISSION);
 (UDiskMountedReceiver, filter);

4. Code

package ;

/**
  * @Project name: UsbTest
  * @Bank Name:
  * @File Name: ReadUDisk
  * @Creator: 25934
  * @Creation time: 2018-07-24 13:50
  * @Description: TODO
  */

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import static .ACTION_USB_PERMISSION;

public class ReadUDisk {
 private  mOnUDiskCallBack = null;
 private Context    mContext;
 private UsbMassStorageDevice[] storageDevices;
 private List&lt;UsbFile&gt; usbFiles = new ArrayList&lt;&gt;();
 private final UsbManager mUsbManager;

 public ReadUDisk(Context context) {
  mContext = context;
  mUsbManager = (UsbManager) (Context.USB_SERVICE);
 }

 /**
   * Accept USB drive insertion and unplugging events
   * @param onUDiskCallBack
   */
 public void setOnUDiskCallBack( onUDiskCallBack) {
  if (mOnUDiskCallBack == null) {
   registerReceiver();
   mOnUDiskCallBack = onUDiskCallBack;
  }
 }

 /**
   * Registered broadcast receiver
   */
 public void registerReceiver() {
  // Listen to otg insertion and unplug  IntentFilter usbDeviceStateFilter = new IntentFilter();
  (UsbManager.ACTION_USB_DEVICE_ATTACHED);
  (UsbManager.ACTION_USB_DEVICE_DETACHED);
  (UDiskMountedReceiver, usbDeviceStateFilter);
  //Register and listen to custom broadcast  IntentFilter filter = new IntentFilter(Constant.ACTION_USB_PERMISSION);
  (UDiskMountedReceiver, filter);
  ("ReadUDisk", "registerReceiver: ");
 }

 /**
   * Log out of broadcast receiver
   */
 public void unReisterReceiver() {
  if (UDiskMountedReceiver != null) {
   (UDiskMountedReceiver);
  }
 }

 /**
   * Check the permissions of the USB device
   * @param device
   * @return
   */
 public boolean checkPerssion(UsbMassStorageDevice device) {
  if (mUsbManager==null){
   return false;
  }
  if ((())) {//If you have it, just read directly whether the device has permissions   return true;
  } else {
   return false;
  }
 }


 /**
   * Read the number of current USB devices
   * @return
   */
 public int getDeviceCount() {
  //Get storage device  UsbMassStorageDevice[] storageDevices =(mContext);
  return ;
 }

 /**
   * Get the usb device according to position
   * @param position
   * @return
   */
 public UsbMassStorageDevice getUsbMassDevice(int position) {
  //Get storage device  UsbMassStorageDevice[] storageDevices =(mContext);
  if (position &gt; ) {
   return null;
  } else {
   return storageDevices[position];
  }
 }

 /**
   * Get all storage devices on usb
   * @return
   */
 public UsbMassStorageDevice[] getUsbMassAllDevice() {
  //Get storage device  UsbMassStorageDevice[] storageDevices =(mContext);
  return storageDevices;
 }

 /**
   * Get the path according to the device
   * @param device
   * @return
   */
 public FileSystem readDevice(UsbMassStorageDevice device) {
  try {
   if (!checkPerssion(device)){ //Check whether there is permission    return null;
   }

   ();//Initialization is required before using the device   Partition partition = ().get(0); //Use only the first partition of the device   FileSystem currentFs = ();
   // (); // Capacity size   // (); // Size used   // (); // Unused size   UsbFile root  = ();//Get the root directory   String deviceName = ();//Get device tag   return currentFs;
  } catch (Exception e) {
   ();
   return null;
  }
 }

 /**
   * Get the file and folder path of the USB drive
   * @param fileSystem
   * @return
   */
 public List&lt;UsbFile&gt; getUsbFiles(FileSystem fileSystem) {
  ();
  try {
   for (UsbFile file : ()
           .listFiles()) { //Add so file and folder paths to the usbFiles array    (file);
   }
   (usbFiles, new Comparator&lt;UsbFile&gt;() {//Simple sorting Folders in front Files in back    @Override
    public int compare(UsbFile oFile1, UsbFile oFile2) {
     return ()
       ? -1
       : 1;
    }
   });
  } catch (IOException e) {
   ();
  }
  return usbFiles;
 }


 private BroadcastReceiver UDiskMountedReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
   String action = ();
   switch (action) {
    case ACTION_USB_PERMISSION: //Custom permission broadcast     if (mOnUDiskCallBack != null) {
      ();
     }
     break;
    case UsbManager.ACTION_USB_DEVICE_ATTACHED: //The USB device inserts the broadcast     if (mOnUDiskCallBack != null) {
      ();
     }
     break;

    case UsbManager.ACTION_USB_DEVICE_DETACHED: //Usb device unbroadcast     if (mOnUDiskCallBack != null) {
      ();
     }
     break;
   }
  }
 };
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.