我有一个桌面 java 应用程序,还有一个 android 应用程序。两个应用程序一起工作。
桌面应用程序中的用户有一个按钮来启动设备数据应用程序与计算机应用程序之间的传输,反之亦然。
所以我需要使用简单的 USB 数据线传输数据,并且没有互联网连接/WiFi/蓝牙/adb。
我找到了两个适用于 Windows 的 Java MTP 库来解决我的问题,以及 android 的 USB 主机/附件功能:
jMTP成功识别我的 Android 设备、文件夹和其他东西
我已成功在计算机中传输文件 ---> 设备,但是当我尝试在设备中传输文件时出现错误 ---> 计算机
我把我的代码放在解释之后。
jusbpmp但我无法传输设备---> 计算机。
USB 主机/附件没有用,因为传输是从桌面应用程序启动的,当我在 android 开发者指南网站上阅读时,它似乎与我需要的不对应,或者如果用户从设备开始传输。
我尝试从 1 周到成功完成这项任务,但似乎我需要帮助。
Java + jMTP 代码
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 异常。
编辑:
Windows 站点针对 hr 错误 ERROR_INVALID_PARAMETER 0x80070057 说:应用程序提供的参数无效。
但我没有看到女巫参数无效
这是用于将数据设备传输到计算机的库的C类的链接,您可以看到我的错误行230。
这是我使用的 jMTP 库。
你能帮我吗,或者用其他方式来做我需要的事情(Usb4Java,libUSB)?我将不胜感激。
提前谢谢。