Monday, 3 March 2014

Java snippet - upload images to DCIM Android Folder in one click (intellij)

Here is simple snippert for uploading images to Android DCIM folder.
First we need create folder for images, I have something like this:

 i (1).jpg  
 i (2).jpg  
 i (3).jpg  
 i (4).jpg  
 i (5).jpg  
 i (6).jpg  
 i (7).jpg  
 i (8).jpg  
 i (9).jpg  

Now, we copy path to this folder, lets say it is:
 "C:\\Users\\Me\\Pictures\\images_face\\";  

If everything is prepared, you can use this code to upload images to DCIM

 public class Main {  
   public static final int SECOND_DELAY = 3;  
   public static final String path = "C:\\Users\\Me\\Pictures\\images_face\\";  
   public static void main(String[] args) throws IOException, InterruptedException {  
     new Main().doStuff();  
   }  
   private void doStuff() throws IOException, InterruptedException {  
     File file = new File(path);  
     Runtime rt = Runtime.getRuntime();  
     for(File f: file.listFiles())  
     {  
       String command = "adb push " + "\"" +path + f.getName() + "\"" + " /sdcard/DCIM";  
       System.out.println(command);  
       Process pr = rt.exec(command);  
       long timeDelay= SECOND_DELAY * 1000;  
       Thread.sleep(timeDelay);  
     }  
   }  
 }  

P.S Make sure your device is connected and adb shell works ok of course! :)