今天就跟大家聊聊有關(guān)怎么在android中通過usb讀取U盤,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元甕安做網(wǎng)站,已為上家服務(wù),為甕安各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
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); //注冊監(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; } } /** * 注冊廣播接收者 */ 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); //注冊監(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)容,你們對怎么在android中通過usb讀取U盤有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享標(biāo)題:怎么在android中通過usb讀取U盤
文章分享:http://muchs.cn/article46/geegeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、企業(yè)建站、品牌網(wǎng)站設(shè)計(jì)、響應(yīng)式網(wǎng)站、網(wǎng)站營銷、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)