1. 程式人生 > >android mtp 獲取單反相機中的照片

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);
    }
}