前一段時間做過一個u盤讀存數(shù)據(jù)的功能鄙早,記錄一下實現(xiàn)代碼删窒。
第一種方法是通過反射的方法獲取掛載路徑舟误,這是從網(wǎng)上找到的,代碼如下:
public static List getUSBPaths(Context con) {//反射獲取路徑
String[] paths = null;
List data = new ArrayList(); // include sd and usb devices
StorageManager storageManager = (StorageManager) con .getSystemService(Context.STORAGE_SERVICE);
try {
paths = (String[]) StorageManager.class.getMethod("getVolumePaths", null).invoke( storageManager, null);
for (String path : paths) {
String state = (String) StorageManager.class.getMethod("getVolumeState", String.class).invoke(storageManager, path);
if (state.equals(Environment.MEDIA_MOUNTED) && !path.contains("emulated")) { data.add(path);
} } }
catch (Exception e) { e.printStackTrace(); }
return data;}
但是這種方法有一個問題魔招,只有在開啟apk之前就把u盤插上設(shè)備才能獲取到晰搀,如果中途插拔u盤了,路徑則不會獲取到办斑,這與我的項目有一點不同外恕,所以后來使用了另一種方法。
第二種方法俄周,是通過系統(tǒng)廣播獲取掛載設(shè)備路徑吁讨,這個也有一個局限性,就是在開啟apk之前u盤就插上了則不會收到廣播峦朗,也就無法獲取到路徑,所以兩種方法各有優(yōu)缺點排龄。
1.首先設(shè)置一個清單配置文件波势,添加兩個東西。
uses-permission android:name="android.hardware.usb.host" android:required="false"
uses-feature android:name="android.hardware.usb.host" android:required="true" />
2.接下來寫一個廣播接收器橄维,用來接收u盤插拔以及權(quán)限監(jiān)聽之類的廣播尺铣。
public class MediaReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case Intent.ACTION_MEDIA_CHECKING:
LogUtil.e(Constant.LOG_TAG,"ACTION_MEDIA_CHECKING");
break;
case Intent.ACTION_MEDIA_MOUNTED: // 獲取掛載路徑, 讀取U盤文件
Uri uri = intent.getData();
if (uri != null) {
String filePath = uri.getPath();
LogUtil.e(Constant.LOG_TAG, filePath);
File file = new File(filePath + File.separator + "hhzt");
if (!file.exists())
FileUtils.makeDir(file);
Constant.file = file.getPath();
LogUtil.e(Constant.LOG_TAG, file.getPath());
File userFile = new File(file.getPath() + File.separator + UserMgr.getUserName());
if (!userFile.exists())
FileUtils.makeDir(userFile);
Constant.fileName = userFile.getPath() + File.separator;
LogUtil.e(Constant.LOG_TAG, "ACTION_MEDIA_MOUNTED:"+userFile.getPath()); }
break;
case Intent.ACTION_MEDIA_EJECT:
LogUtil.e(Constant.LOG_TAG,"ACTION_MEDIA_EJECT");
break;
case Intent.ACTION_MEDIA_UNMOUNTED:
LogUtil.e(Constant.LOG_TAG,"ACTION_MEDIA_UNMOUNTED");
break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED://接收到U盤設(shè)備插入廣播
UsbDevice device_add = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device_add != null) {
Toast.makeText(LVBXApp.getApp(), "U盤已插入", Toast.LENGTH_SHORT).show(); }
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED://接收到U盤設(shè)設(shè)備拔出廣播
Constant.fileName = "";
Toast.makeText(LVBXApp.getApp(), "U盤已拔出", Toast.LENGTH_SHORT).show();
break;
case Constant.ACTION_USB_PERMISSION://接受到自定義廣播
UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); //允許權(quán)限申請
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (usbDevice != null) { //用戶已授權(quán),可以進(jìn)行讀取操作 }
else { Toast.makeText(LVBXApp.getApp(), "沒有插入U盤", Toast.LENGTH_SHORT).show(); } }
else { Toast.makeText(LVBXApp.getApp(), "未獲取到U盤權(quán)限", Toast.LENGTH_SHORT).show(); } break; } } }
廣播接收器寫好以后去注冊就可以獲取到了争舞。
IntentFilter usbDeviceStateFilter = new IntentFilter(Constant.ACTION_USB_PERMISSION); usbDeviceStateFilter.addAction(Intent.ACTION_MEDIA_MOUNTED); usbDeviceStateFilter.addAction(Intent.ACTION_MEDIA_EJECT); usbDeviceStateFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); usbDeviceStateFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED); mediaReceiver = new MediaReceiver(); registerReceiver(mediaReceiver, usbDeviceStateFilter);
剩余操作就和sdcard差不多凛忿。這兩種方法基本就能獲取到掛載的路徑,可以滿足一般的需求了竞川。