您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在android中通過usb讀取U盤,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
1.關(guān)聯(lián) compile ‘com.github.mjdev:libaums:+'
2.權(quán)限設(shè)置
<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" />
3.監(jiān)聽u盤 插入拔出的廣播
//監(jiān)聽otg插入 拔出 IntentFilter usbDeviceStateFilter = new IntentFilter(); usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); mContext.registerReceiver(UDiskMountedReceiver, usbDeviceStateFilter); //注冊(cè)監(jiān)聽自定義廣播 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; } } /** * 注冊(cè)廣播接收者 */ public void registerReceiver() { //監(jiān)聽otg插入 拔出 IntentFilter usbDeviceStateFilter = new IntentFilter(); usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); mContext.registerReceiver(UDiskMountedReceiver, usbDeviceStateFilter); //注冊(cè)監(jiān)聽自定義廣播 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設(shè)備的權(quán)限 * @param device * @return */ public boolean checkPerssion(UsbMassStorageDevice device) { if (mUsbManager==null){ return false; } if (mUsbManager.hasPermission(device.getUsbDevice())) {//有就直接讀取設(shè)備是否有權(quán)限 return true; } else { return false; } } /** * 讀取當(dāng)前usb設(shè)備的數(shù)量 * @return */ public int getDeviceCount() { //獲取存儲(chǔ)設(shè)備 UsbMassStorageDevice[] storageDevices =UsbMassStorageDevice.getMassStorageDevices(mContext); return storageDevices.length; } /** * 根據(jù)position獲取usb設(shè)備 * @param position * @return */ public UsbMassStorageDevice getUsbMassDevice(int position) { //獲取存儲(chǔ)設(shè)備 UsbMassStorageDevice[] storageDevices =UsbMassStorageDevice.getMassStorageDevices(mContext); if (position > storageDevices.length) { return null; } else { return storageDevices[position]; } } /** * 獲取usb上所有的存儲(chǔ)設(shè)備 * @return */ public UsbMassStorageDevice[] getUsbMassAllDevice() { //獲取存儲(chǔ)設(shè)備 UsbMassStorageDevice[] storageDevices =UsbMassStorageDevice.getMassStorageDevices(mContext); return storageDevices; } /** * 根據(jù)設(shè)備獲取路徑 * @param device * @return */ public FileSystem readDevice(UsbMassStorageDevice device) { try { if (!checkPerssion(device)){ //檢查是否有權(quán)限 return null; } device.init();//使用設(shè)備之前需要進(jìn)行 初始化 Partition partition = device.getPartitions().get(0); //僅使用設(shè)備的第一個(gè)分區(qū) FileSystem currentFs = partition.getFileSystem(); // currentFs.getCapacity(); //容量大小 // currentFs.getOccupiedSpace(); //已使用大小 // currentFs.getFreeSpace(); //未使用的大小 UsbFile root = currentFs.getRootDirectory();//獲取根目錄 String deviceName = currentFs.getVolumeLabel();//獲取設(shè)備標(biāo)簽 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數(shù)組中 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: //自定義權(quán)限廣播 if (mOnUDiskCallBack != null) { mOnUDiskCallBack.onPermissionCallBack(); } break; case UsbManager.ACTION_USB_DEVICE_ATTACHED: //usb設(shè)備插入廣播 if (mOnUDiskCallBack != null) { mOnUDiskCallBack.onAttachDeviceCallBack(); } break; case UsbManager.ACTION_USB_DEVICE_DETACHED: //usb設(shè)備拔出廣播 if (mOnUDiskCallBack != null) { mOnUDiskCallBack.onDetachDeviceCallBack(); } break; } } }; }
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
看完上述內(nèi)容,你們對(duì)怎么在android中通過usb讀取U盤有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。