Mtp/Ptp Android
我正在尝试将使用PTP的相机的文件复制到我的平板电脑。我已经使用了android API MTP设备Mtp/Ptp Android,android,usb,mtp,Android,Usb,Mtp,我正在尝试将使用PTP的相机的文件复制到我的平板电脑。我已经使用了android API MTP设备 ,我已请求必要的权限android.mtp.MtpClient.action.USB_权限 我已打开设备,函数返回true,并打开USBConnection Connexion OK 我尝试将相机的所有文件导入平板电脑/mnt/sdcard/tmpFolder上的临时文件夹中。该路径存在于我的平板电脑上,但当我将其提供给ImportFile函数时,出现错误: [日志] MtpDevice: re
,我已请求必要的权限android.mtp.MtpClient.action.USB_权限 我已打开设备,函数返回true,并打开USBConnection Connexion OK 我尝试将相机的所有文件导入平板电脑/mnt/sdcard/tmpFolder上的临时文件夹中。该路径存在于我的平板电脑上,但当我将其提供给ImportFile函数时,出现错误: [日志]
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
我尝试了一个不存在的路径我收到消息:
[日志]
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;
}
对于有相同问题的人: 在github上找到解决方案: MTP客户 关于上述职位, 我下载了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);
该点是importFileobjectId的第二个参数destPath destPath,需要包括文件夹路径+文件名,那么文件名不应更改为原始文件名
但在原始问题作者中,您只需设置文件夹路径
在第二个参数中请求必要的权限,并写入清单。那是哪一个?对于其余部分,您的帖子不清楚您在何时何地确切地为哪些路径发送消息。createTempFolderCamera。函数名错误,因为它不创建文件夹,只返回文件夹的文件对象。Log.dDebug,File exist;。您的意思是“文件夹存在”?android.mtp.MtpClient.action.USB_权限;对于权限。我有/mnt/sdcard/tmpFolder的消息路径exist MtpDevice:readObject:/mnt/sdcard/tmpFolder MtpDevice:open failed for/mnt/sdcard/tmpFolder,我有/mnt/sdcard/tptptp的另一条消息,该消息不存在。