Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Copy Files from Computer to Device #4

Open
GoogleCodeExporter opened this issue Aug 7, 2015 · 3 comments
Open

How to Copy Files from Computer to Device #4

GoogleCodeExporter opened this issue Aug 7, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

Hi,

Can you please share a sample code where a file is copied from Computer to the 
Device.

Original issue reported on code.google.com by [email protected] on 22 Oct 2012 at 7:51

@GoogleCodeExporter
Copy link
Author

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import jmtp.*;
import java.math.*;




public class Test {

    static PortableDeviceFolderObject folderNew;

    public static void main(String[] args) throws FileNotFoundException, IOException
    {
        //initialize variables for createFolder
        BigInteger a = new BigInteger("12345");

        String path="\\test\\xyzz\\1337\\test22";

        String lastpartofpath=path.substring(path.lastIndexOf("\\")+1);

        System.out.println(lastpartofpath);
        PortableDeviceStorageObject storage= getObject();
        PortableDeviceFolderObject folder = null;

        File fileToSend = new File ("C:\\Path\\Object.xy");
        help1(createFolder(path,storage,folder),lastpartofpath).addAudioObject(fileToSend, "artist", "album", a);
    }

    public static PortableDeviceFolderObject createFolder(String s,PortableDeviceStorageObject storage, PortableDeviceFolderObject folder)
    {

                            s=s.substring((s.indexOf("\\")+1),s.length());
                            if(s.indexOf("\\")!=-1)
                                {
                                    String z= s.substring(0,s.indexOf("\\"));
                                    if(folder == null )
                                        {
                                            folderNew = storage.createFolderObject(z);
                                            createFolder(s,storage,folderNew);
                                        }
                                    else
                                        {
                                            folderNew = folderNew.createFolderObject(z);
                                            createFolder(s,storage,folderNew);
                                        }                               
                                }

                return folderNew;
    }

    //one extra iteration of createFolder (required), maybe more clever solution available
    public static PortableDeviceFolderObject help1(PortableDeviceFolderObject folder, String s)
    {

        folder = folder.createFolderObject(s);
        return folder;
    }


    //return storage object
    public static PortableDeviceStorageObject getObject()
    {
    PortableDeviceManager manager = new PortableDeviceManager();
    PortableDevice device = manager.getDevices()[0]; 

    device.open();

    for(PortableDeviceObject object : device.getRootObjects()) {

        if(object instanceof PortableDeviceStorageObject) {
            PortableDeviceStorageObject storage = (PortableDeviceStorageObject)object;
            return storage;
            }              
        }
    manager.getDevices()[0].close();
    return null;
    }


}

Should copy the object.xy to folder path, that will be created (if any1 knows 
how to address folders without creating them before please let me know).

Original comment by [email protected] on 30 Oct 2012 at 7:35

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

Hi, 
I want to send file to my Android devices specific position like 
"Android\data\com.xyz" from computer. 
Note:This folder exists on device.

I tried this code. But I got this error when creating "data" folder on 
"Android" folder :

be.derycke.pieter.com.COMException: Couldn't create the file (0x80004005)

However, when creating "Android\data2\com.xyz" folder ( difference is data2 
folder name )  I didnt get error.

So what's the problem ? 

Original comment by [email protected] on 5 Feb 2015 at 12:15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant