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

How can I query all the folder and file meta data using Google Drive API Services in one time query?


How can I query metadata of the folders and files using Google Drive API Services, in the one-time for all?

I am using this API com.google.apis:google-api-services-drive:v2-rev170-1.20.0 for my app.

 compile('com.google.apis:google-api-services-drive:v2-rev170-1.20.0') {
    exclude group: 'org.apache.httpcomponents'
}

 public List<File> listFilesInApplicationDataFolder() throws Exception {
    List<File> result = new ArrayList<File>();
    Drive.Files.List request = null;
    try {
        request = mService.files().list();
    } catch (IOException e) {
        e.printStackTrace();
        throw new IOException(e);
    }

    request.setQ("'appfolder' in parents");

    do {
        try {
            FileList files = request.execute();

            result.addAll(files.getItems());
            request.setPageToken(files.getNextPageToken());
        } catch (Exception e) {
            System.out.println("An error occurred: " + e);
            request.setPageToken(null);
            throw new Exception(e);
        }

    } while (request.getPageToken() != null &&
            request.getPageToken().length() > 0);

    return result;
}

This method gets all the folder and files from the root folder. But how can I get all the files in the sub-folder, in the one-time query?

My Drive folder structure looks like this:

  appdata
--P_Folder_1
         --S_Folder_1
                 --File_1
                 --File_2
                 --File_3
         --S_Folder_2
               --File_1
               --File_2
               --File_3
        --S_Folder_3
                 --File_1
                 --File_2
                 --File_3
--P_Folder_2
         --S_Folder_1
                 --File_1
                 --File_2
                 --File_3
         --S_Folder_2
                 --File_1
                 --File_2
                 --File_3
        --S_Folder_3
                 --File_1
                 --File_2
                 --File_3

Please give suggestions or doc to reference. Thank you so much!!


Solution

  • I think this SO question can give you idea on how to achieve your problem.

    You can call the DriveFolder.listChildren that can retrieve a collection of metadata for the direct children of the folder. The result will also include metadata for both files and folders. Here is also the complete code example.

    For more information just check the documentation of the API and this SO questions.