使用 Media Transfer Protocol (MTP) 作为替代解决方案。
替代方案示例代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
// 使用 MTP 来启用设备的 USB 大容量存储模式
MtpManager mtpManager = (MtpManager) context.getSystemService(Context.MTP_SERVICE);
if (mtpManager != null && mtpManager.deviceList != null) {
for (UsbDevice usbDevice : mtpManager.deviceList.values()) {
if (usbDevice.getDeviceClass() == UsbConstants.USB_CLASS_MASS_STORAGE) {
mtpManager.setCurrentDevice(usbDevice);
mtpManager.setDeviceProperty(usbDevice.getDeviceId(),
MtpConstants.DEVICE_PROPERTY_USB_MASS_STORAGE_ENABLED, 1);
}
}
}
} else {
// 使用旧版本的方法来启用设备的 USB 大容量存储模式
Intent intent = new Intent();
intent.setAction("android.intent.action.USB_MASS_STORAGE_ENABLE");
context.sendBroadcast(intent);
}