Upload Files to specified folder in Google Drive

15,892

Solution 1

Yea, finally i can upload my whole directory of file to specific folder in google drive simply because of unmatched folderid.

I've worked success in following code by using setParents function.

String folderId = "0BwI6rRcqw8ZURlpleEVsRUhod0U";

File fileContent = new java.io.File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/" + Files[count].getName());

FileContent mediaContent = new FileContent("image/jpeg", fileContent);

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)));

com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();

Solution 2

Folder ID from Browser

The folder ID is displayed in the URL, when you access it in Google Drive in the browser

Solution 3

folderId is the unique identifier of the Folder object, not the nice name like "Storage". You can retrieve the folder's ID using the files().list() API call, possibly with a q attribute to filter the returned results down as needed.

Share:
15,892
user1884888
Author by

user1884888

Updated on June 05, 2022

Comments

  • user1884888
    user1884888 almost 2 years

    I am having a problem on uploading files to an existing folder in google drive. It keeps telling me "xxx" does not exist.

    String folderId = "Storage";
    
    ChildReference newChild = new ChildReference();
    newChild.setId(fileContent.getName());
    
    drive.children().insert(folderId, newChild).execute();
    

    refer from https://developers.google.com/drive/v2/reference/children/insert