我试图将使用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;
}
答案 0 :(得分:1)
关于以上帖子, 我下载了github gallery3d项目, 并查看MtpClient.java的代码, 然后我找到了区别,
github上的代码部分
heatmap
该点是importFile(objectId,destPath)“ destPath”的第二个参数,需要包含文件夹路径+文件名,因此文件名不应更改为原始文件名
但是在origianl问题作者中,您只需设置文件夹路径 在第二个参数中
答案 1 :(得分:0)
对于遇到同样问题的人:
解决方案是(在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();
}