新聞中心
這篇文章主要介紹了Android中App與U盤(pán)通信的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
一、自定義廣播接收器接收 U 盤(pán)相關(guān)的信息
在 U 盤(pán)插入或插出的時(shí)候,系統(tǒng)都會(huì)發(fā)出一條相關(guān)的廣播,所以我們需要自定義廣播接收器,接收這兩條廣播,然后進(jìn)行相應(yīng)的處理。
public class OtgReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); switch (action) { // 接收到 U 盤(pán)插入廣播 case UsbManager.ACTION_USB_DEVICE_ATTACHED: showToast(context, "U 盤(pán)已插入"); // 獲取相關(guān)的 Usb 設(shè)備 UsbDevice attachUsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (attachUsbDevice != null) { // 進(jìn)行權(quán)限申請(qǐng) permissionRequest(); } break; // 接收到 U 盤(pán)拔出廣播 case UsbManager.ACTION_USB_DEVICE_DETACHED: showToast(context, "U 盤(pán)已拔出"); break; default: break; } } }
因?yàn)?Usb 相關(guān)的設(shè)備操作,需要申請(qǐng)相關(guān)的權(quán)限,所以在接收到 U 盤(pán)插入的廣播之后,我們需要進(jìn)行權(quán)限申請(qǐng)。
private void permissionRequest() { // 設(shè)備管理器 UsbManager usbManager = (UsbManager) MainActivity.getContext().get().getSystemService(Context.USB_SERVICE); // 獲取 U 盤(pán)存儲(chǔ)設(shè)備 UsbMassStorageDevice[] storageDevices = UsbMassStorageDevice.getMassStorageDevices(OtgApplication.getContext()); PendingIntent pendingIntent = PendingIntent.getBroadcast(OtgApplication.getContext(), 0, new Intent(ACTION_USB_PERMISSION), 0); // 進(jìn)行權(quán)限申請(qǐng) usbManager.requestPermission(device.getUsbDevice(), pendingIntent); }
可以看到我們?cè)谏暾?qǐng)權(quán)限的時(shí)候,傳入了一個(gè) PendingIntent,PendingIntent 里面?zhèn)魅胛覀冏远x的廣播 ACTION_USB_PERMISSION,等到權(quán)限申請(qǐng)完成,便會(huì)發(fā)出這條廣播,然后我們可以在廣播接收器中接收并處理,從而進(jìn)行后續(xù)的操作。
case ACTION_USB_PERMISSION: UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if (usbDevice != null) { // 讀取 U 盤(pán)相關(guān)的信息 readDevice(getUsbMass(usbDevice)); } else { showToast(context, "沒(méi)有插入 U 盤(pán)"); } } else { showToast(context, "未獲取到 U 盤(pán)權(quán)限"); } break;
為了簡(jiǎn)化相關(guān)的代碼,我導(dǎo)入 Github 上開(kāi)源的 libaums ,所以需要在 build.gradle 里面加上
compile 'com.github.mjdev:libaums:0.5.5'
通過(guò)接收我們自定義的廣播,便可以從 Intent 里面獲取相應(yīng)的包含 U 盤(pán)信息的 UsbDevice
private void readDevice(UsbMassStorageDevice device) { device.init(); // 設(shè)備分區(qū) Partition partition = device.getPartitions().get(0); // 文件系統(tǒng) FileSystem currentFs = partition.getFileSystem(); // 獲取 U 盤(pán)的根目錄 mRootFolder = currentFs.getRootDirectory(); // 獲取 U 盤(pán)的容量 long capacity = currentFs.getCapacity(); // 獲取 U 盤(pán)的剩余容量 long freeSpace = currentFs.getFreeSpace(); // 獲取 U 盤(pán)的標(biāo)識(shí) String volumeLabel = currentFs.getVolumeLabel(); }
二、將文件導(dǎo)入到 U 盤(pán)中
通常我們將手機(jī)跟 U 盤(pán)通過(guò) OTG 線進(jìn)行連接,都是為了將手機(jī)里面的文件導(dǎo)入到 U 盤(pán)中,我們就以圖片為例子,看看怎樣將圖片導(dǎo)出到 U 盤(pán)中。
將圖片導(dǎo)出到 U 盤(pán)中,我們可以通過(guò)流來(lái)實(shí)現(xiàn),先在 U 盤(pán)對(duì)應(yīng)的目錄,創(chuàng)建新的 jpg/png 格式的文件,然后通過(guò) BitmapFactory 將圖片轉(zhuǎn)換成 Bitmap,再進(jìn)一步拿到對(duì)應(yīng)圖片的 ByteArrayOutputStream,最后將對(duì)應(yīng)的字節(jié)寫(xiě)入文件中。
public static void savePictureToUsb(String picturePath, UsbFile root) { UsbFile newDir = root.createDirectory("Haoz" + System.currentTimeMillis()); UsbFile file = newDir.createFile("Haoz" + System.currentTimeMillis() + ".jpg"); Bitmap bitmap = BitmapFactory.decodeFile(picturePath); OutputStream outputStream = new UsbFileOutputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); outputStream.write(out.toByteArray()); outputStream.flush(); outputStream.close(); file.flush(); file.close(); }
可以看到我們傳入圖片的路徑以及 U 盤(pán)的根目錄,便可以將圖片寫(xiě)入到 U 盤(pán)中,在上一節(jié)中,我們已經(jīng)通過(guò)廣播拿到 U 盤(pán)的根目錄,所以直接用就行了。將文件從 U 盤(pán)中導(dǎo)入到手機(jī)中,其實(shí)思路也是一樣的。畢竟當(dāng) U 盤(pán)插入手機(jī)的那一刻,將 U 盤(pán)當(dāng)成手機(jī)的一個(gè)普通的目錄來(lái)處理就行了。
三、該注意的地方
雖然說(shuō),U 盤(pán)跟手機(jī)之間的通信相對(duì)來(lái)說(shuō)不是很難,但其實(shí)也有很多需要注意的地方,也是筆者在開(kāi)發(fā)過(guò)程中踩過(guò)的坑,這里都記錄出來(lái),供大家參考。
3.1 獲取圖片的路徑
我們通過(guò)圖片選擇庫(kù)或者照相機(jī)回調(diào)出來(lái)的,很多時(shí)候都是圖片的 Uri,而要得到圖片對(duì)應(yīng)的 Bitmap 需要的是圖片的真實(shí)路徑,我們可以通過(guò)以下方法進(jìn)行轉(zhuǎn)換。
public static String getPath(ContentResolver resolver, Uri uri) { if (uri == null) { return null; } if (SCHEME_CONTENT.equals(uri.getScheme())) { Cursor cursor = null; try { cursor = resolver.query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null); if (cursor == null || !cursor.moveToFirst()) { return null; } return cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)); } finally { if (cursor != null) { cursor.close(); } } } return uri.getPath(); }
3.2 文件相關(guān)的讀寫(xiě)操作
文件相關(guān)的讀寫(xiě)操作都是比較耗時(shí)的,特別是當(dāng)文件比較大的時(shí)候。所以我們不能在主線程中進(jìn)行文件的讀寫(xiě),必須將其放在子線程中,等 I/O 操作完成了,再轉(zhuǎn)換到主線程中進(jìn)行操作。
3.3 廣播的注冊(cè)與移除
因?yàn)槲覀兪亲远x廣播接收器來(lái)接收相應(yīng)的廣播,所以需要在 Activity 中進(jìn)行廣播的動(dòng)態(tài)注冊(cè),將對(duì)應(yīng) Action 進(jìn)行過(guò)濾。最后不要忘記了在 onDestroy() 中移除廣播,防止內(nèi)存泄露。
private void registerUDiskReceiver() { IntentFilter usbDeviceFileter = new IntentFilter(); usbDeviceFileter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); usbDeviceFileter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); usbDeviceFileter.addAction(Intent.ACTION_MEDIA_MOUNTED); registerReceiver(mOtgReceiver, usbDeviceFileter); // 注冊(cè)監(jiān)聽(tīng)自定義廣播 IntentFilter filter = new IntentFilter(OtgReceiver.ACTION_USB_PERMISSION); registerReceiver(mOtgReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mOtgReceiver); }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android中App與U盤(pán)通信的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
新聞名稱(chēng):Android中App與U盤(pán)通信的示例分析-創(chuàng)新互聯(lián)
本文來(lái)源:http://ef60e0e.cn/article/ddogoe.html