前提・実現したいこと
USBでカメラを接続したAndroidがあります。このカメラはMTPで制御可能です。
発生している問題・エラーメッセージ
MtpDeviceに対するoperationの発行方法がわかりません。具体的には、InitiateCaptureのoperationを発行したいです。これは、カメラの写真を撮影するためです。
該当のソースコード
権限の取得は、すでに行ってあります。
Java
1HashMap<String, UsbDevice> devices = usbManager.getDeviceList(); 2// 実際には、探す作業を行って取得しているが、簡易的に 3UsbDevice targetDevice = devices.get("deviceName"); 4if (targetDevice == null) { 5 // エラー処理を行う 6} 7UsbDeviceConnection connection = usbManager.openDevice(targetDevice); 8if (connection == null) { 9 // エラー処理を行う 10} 11MtpDevice mtpDevice = new MtpDevice(targetDevice); 12if (!mtpDevice.open(connection)) { 13 // エラー処理を行う 14} 15int[] storageIds = mtpDevice.getStorageIds(); 16if (storageIds == null) { 17 // エラー処理を行う 18} 19// 実際には、探す作業を行って取得しているが、簡易的に 20int storageId = storageIds[0]; 21// このあと、どのようなコードを書けばよいのかわかりません 22// コマンドを発行した後は、このような流れになるはずです 23int handleId; 24while (true) { 25 try { 26 MtpEvent event; 27 event = mtpDevice.readEvent(null); 28 if (event.getEventCode() == MtpEvent.EVENT_OBJECT_ADDED) { 29 handleId = event.getParameter1(); 30 break; 31 } 32 } catch (IOException e) { 33 // エラー処理を行う 34 } 35} 36// 外部ストレージに書くならこれ、内部ストレージに書く場合はParcelFileDescriptorを使う 37String targetPath = "/path/to/output"; 38if (mtpDevice.importFile(handleId, targetPath)) { 39 // 成功時の処理 40} else { 41 // エラー処理を行う 42}
試したこと
色々ドキュメントを読んだり、検索をしましたが、見つかりませんでした。
補足情報(FW/ツールのバージョンなど)
- ホスト側のOS Android 7.0
あなたの回答
tips
プレビュー