我正在尝试将使用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
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;
}发布于 2018-07-05 17:31:50
关于上面的帖子,我下载了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“的第二个参数,需要包含文件夹路径+文件名,那么文件名不能改成原来的文件名
但在原始问题作者中,您只需在第二个参数中设置文件夹路径
发布于 2017-01-13 20:37:40
对于有同样问题的人:
解决方案是(在github上找到):
@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();
}https://stackoverflow.com/questions/41596410
复制相似问题