Sunday, 16 December 2012

Upload File to specific folder in Google Drive From Android (Eclipse Development)


First of all, you must put all file+folder into FileRequest and query the folder String as "Storage" (one of my foldername under Google Drive), then obtian folder id by using parent.getId() function and proceed last step to upload files.

 for (int count = 0 ; count < Files.length;count ++){
       
               String folderId = null;
                       
            File fileContent = new java.io.File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/" + Files[count].getName());
            FileContent mediaContent = new FileContent("image/jpeg", fileContent);
         
            Files.List request = drive
                    .files()
                    .list()
                    .setMaxResults(200)
                    .setQ("'root' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false and title='Storage' ");
           
            FileList files = request.execute();
            //ParentReference pr = files.getItems();
           
            for (com.google.api.services.drive.model.File parent : files.getItems()) {
                Log.d(TAG,"folder id:" + parent.getId());
                folderId=parent.getId();
              }



             com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
         body.setTitle(fileContent.getName());
         body.setMimeType("image/jpeg");
       
         body.setParents(
                 Arrays.asList(new ParentReference().setId(folderId)));
     
 
         //--body.setMimeType("video/mp4");
     
         com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();
   
           }

1 comment:

  1. What if I want to upload a SQLite .db or say .ipd (my own extension for sqlite database file) for my app. I have been searching day and night and not getting anything. Please if you can help me with it.

    ReplyDelete