首页 文章

Mtp / Ptp Android

提问于
浏览
0

我正在尝试将使用PTP的相机文件复制到我的平板电脑上 . 我使用了android API MTPDevice(https://developer.android.com/reference/android/mtp/MtpDevice.html#importFile%28int,%20java.lang.String%29),我已经请求了必要的权限(android.mtp.MtpClient.action.USB_PERMISSION) .

我打开设备,函数返回true,然后打开USBConnection(Connexion OK) .

我尝试在平板电脑上的临时文件夹中导入相机的所有文件(/ mnt / sdcard / tmpFolder) . 该路径存在于我的平板电脑上,但是当我将它提供给importFiles函数时,我有错误:

[logcat的]

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

我试过一条路径不存在我有消息:

[logcat的]

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

2 回答

  • 1

    关于上面的帖子,我下载了github gallery3d项目,并查看了MtpClient.java的代码,然后我找到了区别,

    来自github的代码部分

    String destPath = new File(dest,objInfo.getName()).getAbsolutePath();
    int objectId = objInfo.getObjectHandle();
    
    boolean result = mtpClient.getDeviceList().get(i).importFile(objectId, destPath);
    

    点是importFile的第二个参数(objectId,destPath)“destPath”,需要包含文件夹路径文件名,然后文件名不能更改原始文件名

    但在origianl问题作者中,您只需在第二个参数中设置文件夹路径

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

相关问题