Sla*_*sch 4 java usb android wpd mtp
我有一个桌面java应用程序,也是一个Android应用程序.两个app一起工作.
桌面应用程序中的用户有一个按钮来启动设备数据应用程序与计算机应用程序之间的传输,反之亦然.
所以我需要用简单的USB线传输数据,没有互联网连接/ WiFi /蓝牙/ adb.
我找到两个可以在Windows上运行的Java MTP库来解决我的问题,以及android的USB Host/accesory功能:
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();
}
Run Code Online (Sandbox Code Playgroud)
这是代码和错误的结果
`
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
`
Run Code Online (Sandbox Code Playgroud)
我在互联网上读到0x80070057是一个通用的Windows异常.
编辑:
Windows站点说 hr错误ERROR_INVALID_PARAMETER 0x80070057:应用程序提供的参数无效.
但我没有看到女巫参数无效
这里是用于传输数据设备到计算机的库的C类链接,你可以看到我的错误行230.
这是我使用的jMTP库.
你能帮助我,还是用其他方式去做我需要的东西(Usb4Java,libUSB)?我将非常感激.
谢谢你提前.
好的,我发现了问题.
问题来自o2.getID()参数给予方法copy.copyFromPortableDeviceToHost.
因为o2代表文件夹,而不是文件夹中的文件,所以不可能发送文件夹,为了成功我需要在文件夹中发送文件.
所以我将我的PortableDeviceObjecto2转换为a PortableDeviceFolderObject,以便targetFolder.getChildObjects()在PortableDeviceFolderObject表示文件中获取子对象的列表'然后我可以从文件夹迭代任何子对象.
对于每个文件,我copy.copyFromPortableDeviceToHost使用正确的id 调用方法.
这是更正代码,从计算机到设备以及从设备到计算机的复制/传输文件.
我希望它有所帮助.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6473 次 |
| 最近记录: |