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.
So I need to transfer data with a simple USB cable, and without internet connection/WiFi/Bluetooth/adb.
I found two Java MTP library that works on Windows to resolve my problem, and the USB Host/accesory fonctionnality of 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 but i don't have the possibility to transfer device ---> computer.
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.
I try from 1 week to success in this task but it seems i need help.
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();
}
This is the result of the code and the error
`
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
`
I read in internet 0x80070057 is a generic windows exception .
Edit:
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
Here is the link of C class of the library use for transfer data device to computer, you can see my error line 230.
And this is the jMTP library i use.
Can you help me, or purpose an other way to do what i need(Usb4Java, libUSB) ? I shall be really grateful.
Thanks by advance.
Ok, i found the problem.
The problem come from o2.getID()
parameter give to the methode copy.copyFromPortableDeviceToHost
.
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.
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.
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.
I hope it's help.
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);
}
}
}