美文网首页简化开发
Android U盘挂载路径获取

Android U盘挂载路径获取

作者: T_涛_T | 来源:发表于2018-08-29 09:52 被阅读0次

    前一段时间做过一个u盘读存数据的功能,记录一下实现代码。

    第一种方法是通过反射的方法获取挂载路径,这是从网上找到的,代码如下:

    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盘插上设备才能获取到,如果中途插拔u盘了,路径则不会获取到,这与我的项目有一点不同,所以后来使用了另一种方法。

    第二种方法,是通过系统广播获取挂载设备路径,这个也有一个局限性,就是在开启apk之前u盘就插上了则不会收到广播,也就无法获取到路径,所以两种方法各有优缺点。

    1.首先设置一个清单配置文件,添加两个东西。

    uses-permission android:name="android.hardware.usb.host" android:required="false"

    uses-feature android:name="android.hardware.usb.host" android:required="true" />

    2.接下来写一个广播接收器,用来接收u盘插拔以及权限监听之类的广播。

    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盘设备插入广播

    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盘设设备拔出广播

    Constant.fileName = "";

    Toast.makeText(LVBXApp.getApp(), "U盘已拔出", Toast.LENGTH_SHORT).show();

    break;

    case Constant.ACTION_USB_PERMISSION://接受到自定义广播

    UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); //允许权限申请

    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {

    if (usbDevice != null) { //用户已授权,可以进行读取操作 }

    else { Toast.makeText(LVBXApp.getApp(), "没有插入U盘", Toast.LENGTH_SHORT).show(); } }

    else { Toast.makeText(LVBXApp.getApp(), "未获取到U盘权限", 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差不多。这两种方法基本就能获取到挂载的路径,可以满足一般的需求了。

    相关文章

      网友评论

        本文标题:Android U盘挂载路径获取

        本文链接:https://www.haomeiwen.com/subject/ynqhwftx.html