android mtp 获取单反相机中的照片

通过调用Android mtp API 完成相机照片的导出

核心代码:

//注册广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
registerReceiver(mtpReceiver, intentFilter);

//这里通过 广播回调 拿到 usbdevice
UsbDevice usbDevice = data.getParcelableExtra(UsbManager.EXTRA_DEVICE);

UsbDeviceConnection usbDeviceConnection = manager.openDevice(usbDevice);
mUsbDevice = usbDevice;
//然后再拿到mtpdevice
MtpDevice mMtpDevice = new MtpDevice(usbDevice);
//获取存储对象信息
int[] storageIds = mMtpDevice.getStorageIds();
for (int storageId : storageIds) {
    //获取存储对象下照片的handle
    int[] objectHandles = 
    mMtpDevice.getObjectHandles(storageId,MtpConstants.FORMAT_EXIF_JPEG, 0);

    for (int objectHandle : objectHandles) {
    //通过handle获取mtpobj,拿到mtpobj后就能通过api进行照片相关信息的获取了               
    MtpObjectInfo mtpobj =            
    mMtpDevice.getObjectInfo(objectHandle);
    }
}

github地址:https://github.com/ynyao/cameraphoto

你可能感兴趣的:(android mtp 获取单反相机中的照片)