首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mtp/Ptp Android

Mtp/Ptp Android
EN

Stack Overflow用户
提问于 2017-01-12 00:52:45
回答 2查看 1.2K关注 0票数 0

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

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

我尝试在我的平板电脑上的临时文件夹(/mnt/sdcard/tmpFolder)中导入摄像机的所有文件。路径存在于我的平板电脑上,但当我将其提供给importFiles函数时,我得到了以下错误:

LOGCAT

代码语言:javascript
复制
MtpDevice: readObject: /mnt/sdcard/tmpFolder
MtpDevice: open failed for /mnt/sdcard/tmpFolder
Debug: File import KO

我尝试了一个不存在的路径,我得到了这样的信息:

LOGCAT

代码语言:javascript
复制
MtpDevice: readObject: /mnt/sdcard/tptp
MtpDevice: readResponse failed
Debug: File import KO

有人能帮我吗?

谢谢

代码语言:javascript
复制
  @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;
        }
EN

回答 2

Stack Overflow用户

发布于 2018-07-05 17:31:50

关于上面的帖子,我下载了github gallery3d项目,并查看了MtpClient.java的代码,然后我发现了不同之处。

来自github的代码部分

代码语言:javascript
复制
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“的第二个参数,需要包含文件夹路径+文件名,那么文件名不能改成原来的文件名

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

票数 1
EN

Stack Overflow用户

发布于 2017-01-13 20:37:40

对于有同样问题的人:

解决方案是(在github上找到):

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

代码语言:javascript
复制
@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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41596410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档