Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for files (with conditions) in the Google Drive SDK using Java

I'm trying to search for files in the drive with Java, but I'm not sure how to set the conditions. The example given in the video tutorial is in Python. So, basically here is the method that will retrieve the List of files from the Drive:

private static List<File> retrieveAllFiles(Drive service) throws IOException {
    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {

        try {
            FileList files = request.execute();
            result.addAll(files.getItems());
            request.setPageToken(files.getNextPageToken());
            } catch (IOException e) {
                System.out.println("An error occurred: " + e);
                request.setPageToken(null);
            }
    } while (request.getPageToken() != null &&
            request.getPageToken().length() > 0);

    return result;
}

Now, they mention here that the File.List method accepts the q parameter. How can I do that? When I try to set the parameter with examples like the given in the video where q = "title contains 'fruit'", it does not work. What am I doing wrong? Is the request.queue() used for this?

Thanks

like image 666
Luis Lavieri Avatar asked Jul 21 '13 17:07

Luis Lavieri


2 Answers

Use setQ method for Files list request.

https://developers.google.com/resources/api-libraries/documentation/drive/v2/java/latest/com/google/api/services/drive/Drive.Files.List.html#setQ(java.lang.String)

For examples on how to query for files/folders:

https://developers.google.com/drive/web/search-parameters#examples

like image 156
Ali Afshar Avatar answered Oct 12 '22 19:10

Ali Afshar


I found it. I think the Google Drive SDK documentation needs to be a little less ambiguous.

Files.List request = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'");
like image 37
Luis Lavieri Avatar answered Oct 12 '22 18:10

Luis Lavieri