android u盘加载_android通过usb读取U盘的方法

本文介绍了如何在Android中通过USB接口读取U盘的详细步骤,包括添加库、设置权限、监听U盘插入拔出事件以及读取U盘内容的方法。提供了一个完整的ReadUDisk类实现,涵盖设备权限检查、文件系统读取和文件路径获取等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文实例为大家分享了android通过usb读取U盘的具体代码,供大家参考,具体内容如下

1.关联 compile ‘com.github.mjdev:libaums:+'

2.权限设置

3.监听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;

/**

* @项目名: UsbTest

* @包名: com.zb.usbtest

* @文件名: ReadUDisk

* @创建者: 25934

* @创建时间: 2018-07-24 13:50

* @描述: TODO

*/

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 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() {

//监听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);

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();

// currentFs.getCapacity(); //容量大小

// currentFs.getOccupiedSpace(); //已使用大小

// currentFs.getFreeSpace(); //未使用的大小

UsbFile root = currentFs.getRootDirectory();//获取根目录

String deviceName = currentFs.getVolumeLabel();//获取设备标签

return currentFs;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

/**

* 获取U盘的文件和文件夹路径

* @param fileSystem

* @return

*/

public List getUsbFiles(FileSystem fileSystem) {

usbFiles.clear();

try {

for (UsbFile file : fileSystem.getRootDirectory()

.listFiles()) { //将所以文件和文件夹路径添加到usbFiles数组中

usbFiles.add(file);

}

Collections.sort(usbFiles, new Comparator() {//简单排序 文件夹在前 文件在后

@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: //usb设备插入广播

if (mOnUDiskCallBack != null) {

mOnUDiskCallBack.onAttachDeviceCallBack();

}

break;

case UsbManager.ACTION_USB_DEVICE_DETACHED: //usb设备拔出广播

if (mOnUDiskCallBack != null) {

mOnUDiskCallBack.onDetachDeviceCallBack();

}

break;

}

}

};

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: android通过usb读取U盘的方法

本文地址: http://www.cppcns.com/ruanjian/android/235761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值