Desktop java app copy and transfer android data via USB

こ雲淡風輕ζ 提交于 2019-12-03 03:50:36

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);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!