Android 获取U盘挂载路径

在Android系统中,我们经常需要获取外接设备的挂载路径,比如U盘。本文将介绍如何在Android应用程序中获取U盘的挂载路径,并提供了相应的代码示例。

1. USB权限声明

首先,我们需要在AndroidManifest.xml文件中声明USB权限,以便应用程序可以访问USB设备。在<manifest>标签内添加以下权限声明:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-feature android:name="android.hardware.usb.host" />

2. 检测U盘插入和拔出事件

当U盘插入或拔出时,我们需要监听相应的事件。我们可以通过注册广播接收器来实现。

public class UsbReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            // U盘插入事件
            UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            // 处理U盘插入逻辑
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            // U盘拔出事件
            UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            // 处理U盘拔出逻辑
        }
    }
}

在AndroidManifest.xml文件中注册广播接收器:

<receiver
    android:name=".UsbReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
    </intent-filter>
</receiver>

3. 获取U盘挂载路径

当检测到U盘插入事件时,我们可以通过UsbDevice对象获取U盘的挂载路径。

public String getUsbMountPath(UsbDevice device) {
    String usbPath = "";

    StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
    if (storageManager != null) {
        StorageVolume[] storageVolumes = storageManager.getVolumeList();
        for (StorageVolume volume : storageVolumes) {
            String path = volume.getPath();
            if (path.contains("usb") && volume.isRemovable()) {
                usbPath = path;
                break;
            }
        }
    }

    return usbPath;
}

4. 完整代码示例

下面是一个完整的示例代码,演示了如何获取U盘的挂载路径:

public class MainActivity extends AppCompatActivity {

    private UsbReceiver usbReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        usbReceiver = new UsbReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(usbReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(usbReceiver);
    }

    public String getUsbMountPath(UsbDevice device) {
        String usbPath = "";

        StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
        if (storageManager != null) {
            StorageVolume[] storageVolumes = storageManager.getVolumeList();
            for (StorageVolume volume : storageVolumes) {
                String path = volume.getPath();
                if (path.contains("usb") && volume.isRemovable()) {
                    usbPath = path;
                    break;
                }
            }
        }

        return usbPath;
    }

    public class UsbReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                // U盘插入事件
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                String usbPath = getUsbMountPath(device);
                if (!TextUtils.isEmpty(usbPath)) {
                    // 处理U盘插入逻辑
                    Log.d("USB", "U盘已插入,挂载路径:" + usbPath);
                }
            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                // U盘拔出事件
                UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                String usbPath = getUsbMountPath(device);
                if (!TextUtils.isEmpty(usbPath)) {