2017-01-11 15 views
0

PTP를 사용하는 카메라의 파일을 내 태블릿에 복사하려고합니다. 나는 안드로이드 API MTPDevice (https://developer.android.com/reference/android/mtp/MtpDevice.html#importFile%28int,%20java.lang.String%29)을 사용하고 있습니다. 필요한 권한 (android.mtp.MtpClient.action.USB_PERMISSION)을 요청했습니다.Mtp/Ptp Android

장치를 열고 함수가 true를 반환하고 USBConnection (연결 확인)을 엽니 다.

태블릿의 임시 폴더 (/ mnt/sdcard/tmpFolder)에서 카메라의 모든 파일을 가져 오려고합니다. 경로는 내 태블릿에 존재하지만, 내가 importFiles 기능에게 줄 때 오류가 : 나는 메시지가 존재하지 않는 경로로 시도가

[로그 캣]

MtpDevice: readObject: /mnt/sdcard/tmpFolder 
MtpDevice: open failed for /mnt/sdcard/tmpFolder 
Debug: File import KO 

를 :

[로그 캣]

MtpDevice: readObject: /mnt/sdcard/tptp 
MtpDevice: readResponse failed 
Debug: File import KO 

누군가가 나를 도울 수 있습니까?

덕분에 같은 문제가있는 사람들을 위해

@Background 
@DebugLog 
public void getMTPDevice() { 
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList(); 
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 
    if (deviceIterator.hasNext()) { 
     UsbDevice usbDevice = deviceIterator.next(); 
     device = openDeviceLocked(usbDevice); 
     if(device!=null){ 
       File folder = returnTempFolderCamera(); 
       if(folder.exists()){ 
        Log.d("Debug", "Folder exist /mnt/sdcard/tmpFolder"); 
        if(device.importFile(0,folder.getPath())) 
        { 
         Toast.makeText(this, "File import OK", Toast.LENGTH_LONG).show(); 
         Log.d("Debug", "Files import OK"); 
        }else { 
         Toast.makeText(this, "File import KO", Toast.LENGTH_LONG).show(); 
         Log.d("Debug", "Files import KO"); 
        } 
       } 
     } 
    } 
}/** 
* Opens the {@link android.hardware.usb.UsbDevice} for an MTP or PTP device 
* and return an {@link android.mtp.MtpDevice} for it. 
* 
* @param usbDevice 
*   the device to open 
* @return an MtpDevice for the device. 
*/ 
@DebugLog 
private MtpDevice openDeviceLocked(UsbDevice usbDevice) { 
    String deviceName = usbDevice.getDeviceName(); 
    byte[] data = new byte[128]; 
    int TIMEOUT = 0; 
    boolean forceClaim = true; 
    // don't try to open devices that we have decided to ignore 
    // or are currently asking permission for 
    if (isCamera(usbDevice) 
      && !mRequestPermissionDevices.contains(deviceName)) { 
     if (!manager.hasPermission(usbDevice)) { 
      manager.requestPermission(usbDevice, mPermissionIntent); 
      mRequestPermissionDevices.add(deviceName); 
     } else { 
      UsbInterface intf = usbDevice.getInterface(0); 
      UsbEndpoint endpoint = intf.getEndpoint(0); 
      UsbDeviceConnection connection = manager.openDevice(usbDevice); 
      connection.claimInterface(intf, forceClaim); 
      connection.bulkTransfer(endpoint, data, data.length, TIMEOUT); 
      if (connection != null) { 
       MtpDevice mtpDevice = new MtpDevice(usbDevice); 
       if (mtpDevice.open(connection)) { 
        mDevices.put(usbDevice.getDeviceName(), mtpDevice); 
        return mtpDevice; 
       } 
      } 
     } 
    } 
    return null; 
} 
     private File returnTempFolder(){ 
      File tmp = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tmpFolder"); 
      return tmp; 
     } 
+0

'필요한 요청을 허락을 받고, 매니페스트에 써라. 그게 어떤 것일까? 나머지 부분에서는 경로가 정확히 어디에 있는지 언제 어디에서 메시지가 표시되는지 꽤 명확하지 않습니다. – greenapps

+0

'createTempFolderCamera()'. 잘못된 함수 이름은 폴더를 만들지 않고 폴더의 File 객체 만 반환하기 때문입니다. – greenapps

+0

'Log.d ("디버그", "파일 존재");'. '폴더가 있음'을 의미합니까? – greenapps

답변

0

:

솔루션은 (GitHub의에 있음)된다

MtpClient은 (https://android.googlesource.com/platform/packages/apps/Gallery2/+/jb-dev/src/com/android/gallery3d/data/MtpClient.java)

@Background 
@DebugLog 
public void importFiles() { 
    MtpClient mtpClient = new MtpClient(this); 
    mtpClient.getDeviceList(); 
    for (int i = 0; i < mtpClient.getDeviceList().size(); i++) { 
     int[] tab = mtpClient.getDeviceList().get(i).getObjectHandles(mtpClient.getDeviceList().get(i).getStorageIds()[0], 0, 0); 
     for (int j = 0; j < tab.length; j++) { 
      File dest = Environment.getExternalStorageDirectory(); 
      // NAME_IMPORTED_FOLDER = tmpFolder 
      dest = new File(dest, NAME_IMPORTED_FOLDER); 
      dest.mkdirs(); 
      MtpObjectInfo objInfo = mtpClient.getDeviceList().get(i).getObjectInfo(tab[j]); 
      if (objInfo != null) { 
       String destPath = new File(dest, objInfo.getName()).getAbsolutePath(); 
       int objectId = objInfo.getObjectHandle(); 
       // Succes !! 
       boolean result = mtpClient.getDeviceList().get(i).importFile(objectId, destPath); 
      } 
     } 
    } 
    mtpClient.close(); 
}