在连接的移动设备上读取和写入数据

时间:2017-08-17 12:35:47

标签: java file mobile

我正在尝试创建一个jar客户端,用于读取连接到用户PC的Android平板电脑中的.db文件。如今,我必须手动将文件移动到Windows文件系统,然后手动移回文件。

尝试使用jmtp,但没有用。

我的任务是读取文件,将文件复制到设备上的其他位置并执行一些操作。

我该怎么做?

    PortableDeviceManager manager = new PortableDeviceManager();
    PortableDevice device = manager.getDevices()[0];
    // Connect to my mp3-player
    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().equals("copagazmobile")){

                    PortableDeviceFolderObject storage1 = (PortableDeviceFolderObject) o2;
                    for (PortableDeviceObject o3 : storage1.getChildObjects()) {
                        if(o3.getOriginalFileName().equals("backupdatabases")){
                            PortableDeviceFolderObject folder = (PortableDeviceFolderObject) o3;
                            System.out.println(o3);
                            System.out.println(folder);
                        }
                    }
                    System.out.println(o2.getOriginalFileName());
                }
            }
        }
    }

    manager.getDevices()[0].close();

1 个答案:

答案 0 :(得分:2)

有必要使用我正在使用的名为jmtpe的lib 我用来读取文件并发送给主机的代码:

    PortableDeviceManager manager = new PortableDeviceManager();
    PortableDevice device = manager.getDevices()[0];

    device.open();

    System.out.println("Dispositivo: " + device.getModel());

    System.out.println("---------------");
    System.out.println("Recuperando arquivo do Tablet");

    // 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().equals("copagazmobile")){

                    PortableDeviceFolderObject storage1 = (PortableDeviceFolderObject) o2;
                    for (PortableDeviceObject o3 : storage1.getChildObjects()) {
                        if(o3.getOriginalFileName().equals("backupdatabases")){
                            PortableDeviceFolderObject storage2 = (PortableDeviceFolderObject) o3;

                            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_hhmm");
                            Calendar calendar = Calendar.getInstance();
                            calendar.set(Calendar.YEAR, 1900);
                            Date data = calendar.getTime();
                            String idFile = null;
                            String fileName = null;
                            for (PortableDeviceObject o4 : storage2.getChildObjects()) {
                                String dataArq = (o4.getOriginalFileName().substring(15, 28));

                                try {
                                    if(new Date(format.parse(dataArq).getTime()).after(data)){
                                        data = new Date(format.parse(dataArq).getTime());
                                        idFile = o4.getID();
                                        fileName = o4.getOriginalFileName();
                                    }
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }
                            PortableDeviceToHostImpl32 host =  new PortableDeviceToHostImpl32();
                            try {
                                File f = new File( "c://sqlite//db");
                                System.out.println("Limpando diretório de manipulação: " + f);
                                FileUtils.cleanDirectory(f); 
                                System.out.println("Copiando arquivo: " + fileName );
                                host.copyFromPortableDeviceToHost(idFile, "c:/sqlite/db", device);
                                f = new File( "c://sqlite//db//"+fileName);

                                File novoArquivo = new File( "c://sqlite//db//copagaz_mobile_desenv.db");

                                InputStream inStream = null;
                                OutputStream outStream = null;
                                inStream = new FileInputStream(f);
                                outStream = new FileOutputStream(novoArquivo);

                                byte[] buffer = new byte[1024];
                                int length;
                                while ((length = inStream.read(buffer)) > 0){
                                    outStream.write(buffer, 0, length);
                                }
                                inStream.close();
                                outStream.close();
                                f.delete();


                            } catch (COMException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            System.out.println("Arquivo mais recente é de: " + data);
                        }
                    }
                }
            }
        }
    }

    manager.getDevices()[0].close();

并发回文件:

    PortableDeviceManager manager = new PortableDeviceManager();
    PortableDevice device = manager.getDevices()[0];

    device.open();

    System.out.println("---------------");
    System.out.println("Enviando arquivo para o Tablet");

    // 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().equals("copagazmobile")){

                    PortableDeviceFolderObject storage1 = (PortableDeviceFolderObject) o2;
                    for (PortableDeviceObject o3 : storage1.getChildObjects()) {
                        if(o3.getOriginalFileName().equals("restore")){

                            PortableDeviceFolderObject host =  (PortableDeviceFolderObject) o3;
                            try {
                                File f = new File( "c://sqlite//db//copagaz_mobile_desenv.db");
                                System.out.println("Copiando arquivo .db para o Tablet");
                                host.addAudioObject(f, "0", "1", new BigInteger("12345"));
                                System.out.println("Arquivo copiado");

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }

    manager.getDevices()[0].close();