桌面java应用程序通过USB复制和传输android数据 [英] Desktop java app copy and transfer android data via USB
问题描述
我有一个桌面java应用程序,还有一个Android应用程序。两个应用程序一起工作。
I have a desktop java app, and also an android app. Two app work together.
桌面应用程序中的用户有一个按钮,用于启动设备数据应用程序与计算机应用程序之间的传输,反之亦然。
The user in desktop app have a button to launch the transfer between device data app to computer app and vice versa.
所以我需要用简单的USB线传输数据,没有互联网连接/ WiFi /蓝牙/ adb。
So I need to transfer data with a simple USB cable, and without internet connection/WiFi/Bluetooth/adb.
我找到两个Java在Windows上解决我的问题的MTP库,以及android的USB主机/附件功能:
I found two Java MTP library that works on Windows to resolve my problem, and the USB Host/accesory fonctionnality of android:
-
jMTP 成功识别我的Android设备,文件夹和其他内容
jMTP successfully recognizes my Android devices, folder, and other things
我已成功在计算机中传输文件--->设备,但我有错误当我尝试在设备中传输文件--->计算机
I have success to transfer a file in computer ---> device, but i have an error when i try to transfer a file in device ---> computer
我在解释后放了我的代码。
I put my code after the explaination.
jusbpmp 但我无法转移设备--->电脑。
jusbpmp but i don't have the possibility to transfer device ---> computer.
USB主机/附件无用,因为传输是从桌面应用程序启动的,当我阅读时在android开发者指南网站上,它似乎与我需要的不一致,或者可能是用户从设备开始传输。
USB Host/accesory not usefull because transfer are launch from desktop app, and when i read on the android developper guide website, it seems to be not correspond from what i need, or maybe if the user start transfer from the device.
我尝试从1周到此任务成功,但似乎我需要帮助。
I try from 1 week to success in this task but it seems i need help.
Java + jMTP代码
Java + jMTP code
private static void jMTPeMethode()
{
PortableDeviceManager manager = new PortableDeviceManager();
PortableDevice device = manager.getDevices()[0];
// Connect to USB tablet
device.open();
System.out.println(device.getModel());
System.out.println("---------------");
// Iterate over deviceObjects
for (PortableDeviceObject object : device.getRootObjects())
{
// If the object is a storage object
if (object instanceof PortableDeviceStorageObject)
{
PortableDeviceStorageObject storage = (PortableDeviceStorageObject) object;
for (PortableDeviceObject o2 : storage.getChildObjects())
{
if(o2.getOriginalFileName().equalsIgnoreCase("Test"))
{
//Device to computer not working
PortableDeviceToHostImpl32 copy = new PortableDeviceToHostImpl32();
try
{
copy.copyFromPortableDeviceToHost(o2.getID(), "C:\\TransferTest", device);
} catch (COMException ex)
{
}
// //Host to Device working
// BigInteger bigInteger1 = new BigInteger("123456789");
// File file = new File("c:/GettingJMTP.pdf");
// try {
// storage.addAudioObject(file, "jj", "jj", bigInteger1);
// } catch (Exception e) {
// //System.out.println("Exception e = " + e);
// }
}
System.out.println(o2.getOriginalFileName());
}
}
}
manager.getDevices()[0].close();
}
这是代码和错误的结果
`
Nexus 9
---------------
Music
Podcasts
Ringtones
Alarms
Notifications
Pictures
Movies
Download
DCIM
Android
! Failed to get IStream (representing object data on the device) from IPortableDeviceResources, hr = 0x80070057
test
ReleveData
`
我在互联网上读到0x80070057是一个通用的Windows异常。
I read in internet 0x80070057 is a generic windows exception .
编辑:
Windows网站说小时错误
ERROR_INVALID_PARAMETER 0x80070057:
应用程序提供的参数无效。
Windows site say for the hr error ERROR_INVALID_PARAMETER 0x80070057 : The parameter supplied by the application is not valid.
但是我没有看到女巫参数无效
But i don't see witch parameter is not valid
这是 link C类的库用于将数据设备传输到计算机,你可以看到我的错误行230.
Here is the link of C class of the library use for transfer data device to computer, you can see my error line 230.
和这是我使用的jMTP库。
And this is the jMTP library i use.
你能帮助我,还是用其他方式去做我需要的东西(Usb4Java,libUSB)?我将非常感激。
Can you help me, or purpose an other way to do what i need(Usb4Java, libUSB) ? I shall be really grateful.
感谢提前。
推荐答案
好的,我发现了问题。
问题来自 o2.getID()
参数给出的方法 copy.copyFromPortableDeviceToHost
。
The problem come from o2.getID()
parameter give to the methode copy.copyFromPortableDeviceToHost
.
因为 o2
代表文件夹,而不是文件夹中的文件,因此无法发送文件夹,为了成功,我需要在文件夹中发送文件。
Because o2
representing the folder, and not the file in the folder, so it's not possible to send folder, for success i need to send file in folder.
所以我投了 PortableDeviceObject
o2到 PortableDeviceFolderObject
,以便获取带有 targetFolder.getChildObjects()$的子对象列表c $ c>在
PortableDeviceFolderObject
表示文件'然后我可以从文件夹迭代任何子对象。
So i cast my PortableDeviceObject
o2 to an PortableDeviceFolderObject
, so that get a list of child Object with targetFolder.getChildObjects()
in the PortableDeviceFolderObject
representing files' and then i can iterate on any child objet from the folder.
对于每个文件,我使用正确的id调用方法 copy.copyFromPortableDeviceToHost
。
And for each file i call the methode copy.copyFromPortableDeviceToHost
, with the right id.
这是更正代码,从计算机复制/传输文件到开发冰和设备到计算机。
Here is the correction code, copy/transfer file from computer to device and device to computer.
我希望它有所帮助。
public class USBTransfertMain {
public static void main(String[] args) throws Throwable {
jMTPeMethode();
}
private static void jMTPeMethode()
{
PortableDeviceFolderObject targetFolder = null;
PortableDeviceManager manager = new PortableDeviceManager();
PortableDevice device = manager.getDevices()[0];
// Connect to USB tablet
device.open();
System.out.println(device.getModel());
System.out.println("---------------");
// Iterate over deviceObjects
for (PortableDeviceObject object : device.getRootObjects())
{
// If the object is a storage object
if (object instanceof PortableDeviceStorageObject)
{
PortableDeviceStorageObject storage = (PortableDeviceStorageObject) object;
for (PortableDeviceObject o2 : storage.getChildObjects())
{
if(o2.getOriginalFileName().equalsIgnoreCase("testFolder"))
{
targetFolder = (PortableDeviceFolderObject) o2;
}
System.out.println(o2.getOriginalFileName());
}
copyFileFromComputerToDeviceFolder(targetFolder);
PortableDeviceObject[] folderFiles = targetFolder.getChildObjects();
for (PortableDeviceObject pDO : folderFiles) {
copyFileFromDeviceToComputerFolder(pDO, device);
}
}
}
manager.getDevices()[0].close();
}
private static void copyFileFromDeviceToComputerFolder(PortableDeviceObject pDO, PortableDevice device)
{
PortableDeviceToHostImpl32 copy = new PortableDeviceToHostImpl32();
try {
copy.copyFromPortableDeviceToHost(pDO.getID(), "C:\\TransferTest", device);
} catch (COMException ex) {
ex.printStackTrace();
}
}
private static void copyFileFromComputerToDeviceFolder(PortableDeviceFolderObject targetFolder)
{
BigInteger bigInteger1 = new BigInteger("123456789");
File file = new File("C:\\GettingJMTP.pdf");
try {
targetFolder.addAudioObject(file, "jj", "jj", bigInteger1);
} catch (Exception e) {
System.out.println("Exception e = " + e);
}
}
}
这篇关于桌面java应用程序通过USB复制和传输android数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!