Search code examples
javaandroidgoogle-drive-apigoogle-drive-android-api

How to make folders within folders in Google Drive?


I know how to make a folder in Google Drive using the API v2 for Java: https://developers.google.com/drive/v3/web/folder#creating_a_folder

But how do I create a folder within the folder, to put my file in it?

Let's say I have a file myfile that I want to upload to /myfolder1/myfolder2/ directory in Google Drive, should I first create myfolder1, then get its ID, then create myfolder2 and set its parent to myfolder1, then set the parent of myfile to myfolder1? Is there a better way to do it?

I am adding the code too:

private File insertFile(String filePath) {
    // Set file's metadata on the server
    File body = new File();
    // Title of the file to insert, including the extension.
    body.setTitle(getFileName(filePath) + "." + getExtension(filePath));
    body.setMimeType(null);
    body.setFileExtension(getExtension(filePath));

    // folderList is string array of the name of the parent folders for the  file
    String[] folderList = getFoldersList(filePath);

    File folder1 = new File();
    folder1.setMimeType("application/vnd.google-apps.folder");
    folder1.setTitle(folderList[0]);
    try {
        folder1 = this.service.files().insert(folder1).execute();
    }
    catch (IOException e){
        e.printStackTrace();
    }

    File folder2 = new File();
    folder2.setMimeType("application/vnd.google-apps.folder");
    folder2.setTitle(folderList[1]);
    folder2.setParents(Arrays.asList(new ParentReference().setId(folder1.getId())));
    try {
        folder2 = this.service.files().insert(folder2).execute();
    }
    catch (IOException e){
        e.printStackTrace();
    }

    body.setParents(Arrays.asList(new ParentReference().setId(folder2.getId())));


    // Set file's content.
    java.io.File fileContent = new java.io.File(filePath);
    // Set file's type
    FileContent mediaContent = new FileContent(null, fileContent);

    try {
        File file = this.service.files().insert(body, mediaContent).execute();
        return file;
    } catch (IOException e) {
        Log.v(DEBUG_TAG, "An error occurred while uploading.");
        e.printStackTrace();
        return null;
    }
}

Thanks


Solution

  • Here is a little function that takes care of it, you can use the returned ID as the parentId of the file you are creating, and the file will be created inside multiple folders :)

    // This function gets an array of Strings representing a file's parent folders
    // And returns the most inner parent folder's ID
    private String makeParentFolders(String[] parentList) {
    
        File folder = null;
        String folderId = null;
        for(int i = 0; i < parentList.length; i++) {
            folder = new File();
            folder.setMimeType(getMimeType("folder"));
            folder.setTitle(parentList[i]);
            if (i > 0) {
                folder.setParents(Arrays.asList(new ParentReference().setId(folderId)));
            }
            try {
                folder = this.service.files().insert(folder).execute();
                folderId = folder.getId();
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
    
        return folder.getId();
    }
    

    If anyone comes up with a more elegant way, please post here.