- 关联 compile ‘com.github.mjdev:libaums:+’
- 权限设置
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.hardware.usb.host" android:required="false" />
<uses-feature android:name="android.hardware.usb.host" android:required="true" />
- 监听u盘 插入拔出的广播
//监听otg插入 拔出
IntentFilter usbDeviceStateFilter = new IntentFilter()
usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
mContext.registerReceiver(UDiskMountedReceiver, usbDeviceStateFilter)
//注册监听自定义广播
IntentFilter filter = new IntentFilter(Constant.ACTION_USB_PERMISSION)
mContext.registerReceiver(UDiskMountedReceiver, filter)
-
4.
package com.zb.usbtest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.util.Log;
import com.github.mjdev.libaums.UsbMassStorageDevice;
import com.github.mjdev.libaums.fs.FileSystem;
import com.github.mjdev.libaums.fs.UsbFile;
import com.github.mjdev.libaums.partition.Partition;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static com.zb.usbtest.Constant.ACTION_USB_PERMISSION;
public class ReadUDisk {
private UDiskCallBack.OnUDiskCallBack mOnUDiskCallBack = null;
private Context mContext;
private UsbMassStorageDevice[] storageDevices;
private List<UsbFile> usbFiles = new ArrayList<>();
private final UsbManager mUsbManager;
public ReadUDisk(Context context) {
mContext = context;
mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
}
/**
* 接受U盘插入和拔出事件
* @param onUDiskCallBack
*/
public void setOnUDiskCallBack(UDiskCallBack.OnUDiskCallBack onUDiskCallBack) {
if (mOnUDiskCallBack == null) {
registerReceiver();
mOnUDiskCallBack = onUDiskCallBack;
}
}
/**
* 注册广播接收者
*/
public void registerReceiver() {
IntentFilter usbDeviceStateFilter = new IntentFilter();
usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
mContext.registerReceiver(UDiskMountedReceiver, usbDeviceStateFilter);
IntentFilter filter = new IntentFilter(Constant.ACTION_USB_PERMISSION);
mContext.registerReceiver(UDiskMountedReceiver, filter);
Log.e("ReadUDisk", "registerReceiver: ");
}
/**
* 注销广播接收者
*/
public void unReisterReceiver() {
if (UDiskMountedReceiver != null) {
mContext.unregisterReceiver(UDiskMountedReceiver);
}
}
/**
* 检查usb设备的权限
* @param device
* @return
*/
public boolean checkPerssion(UsbMassStorageDevice device) {
if (mUsbManager==null){
return false;
}
if (mUsbManager.hasPermission(device.getUsbDevice())) {
return true;
} else {
return false;
}
}
/**
* 读取当前usb设备的数量
* @return
*/
public int getDeviceCount() {
UsbMassStorageDevice[] storageDevices =UsbMassStorageDevice.getMassStorageDevices(mContext);
return storageDevices.length;
}
/**
* 根据position获取usb设备
* @param position
* @return
*/
public UsbMassStorageDevice getUsbMassDevice(int position) {
UsbMassStorageDevice[] storageDevices =UsbMassStorageDevice.getMassStorageDevices(mContext);
if (position > storageDevices.length) {
return null;
} else {
return storageDevices[position];
}
}
/**
* 获取usb上所有的存储设备
* @return
*/
public UsbMassStorageDevice[] getUsbMassAllDevice() {
UsbMassStorageDevice[] storageDevices =UsbMassStorageDevice.getMassStorageDevices(mContext);
return storageDevices;
}
/**
* 根据设备获取路径
* @param device
* @return
*/
public FileSystem readDevice(UsbMassStorageDevice device) {
try {
if (!checkPerssion(device)){
return null;
}
device.init();
Partition partition = device.getPartitions().get(0);
FileSystem currentFs = partition.getFileSystem();
UsbFile root = currentFs.getRootDirectory();
String deviceName = currentFs.getVolumeLabel();
return currentFs;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取U盘的文件和文件夹路径
* @param fileSystem
* @return
*/
public List<UsbFile> getUsbFiles(FileSystem fileSystem) {
usbFiles.clear();
try {
for (UsbFile file : fileSystem.getRootDirectory()
.listFiles()) {
usbFiles.add(file);
}
Collections.sort(usbFiles, new Comparator<UsbFile>() {
@Override
public int compare(UsbFile oFile1, UsbFile oFile2) {
return oFile1.isDirectory()
? -1
: 1;
}
});
} catch (IOException e) {
e.printStackTrace();
}
return usbFiles;
}
private BroadcastReceiver UDiskMountedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case ACTION_USB_PERMISSION:
if (mOnUDiskCallBack != null) {
mOnUDiskCallBack.onPermissionCallBack();
}
break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
if (mOnUDiskCallBack != null) {
mOnUDiskCallBack.onAttachDeviceCallBack();
}
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED:
if (mOnUDiskCallBack != null) {
mOnUDiskCallBack.onDetachDeviceCallBack();
}
break;
}
}
};
}