Querying for Files

You can use the com.google.android.gms.drive.query package to search a user's Drive account for files whose metadata match your search criteria. You can issue a query for a specific folder or on the entire filesystem.

Building queries

A query is created by building an instance of the Query class and specifying the search criteria with Filters. The following example finds all files with the title "HelloWorld.java".

Query query = new Query.Builder()
        .addFilter(Filters.eq(SearchableField.TITLE, "HelloWorld.java"))

You can use the Filters class to build expressions. Multiple filters can be joined together using the and and or methods.

Once a Query object has been constructed it can be executed on the entire file system using Drive.DriveApi as follows:

Drive.DriveApi.query(googleApiClient, query);

This query starts in the My Drive (the root) folder and recursively traverse the entire filesystem, returning all entries matching the Filters expression tree.

A query can alternatively be executed only in a specific folder using an instance of the DriveFolder class, as shown by:

DriveFolder folder= ...;

This call does not scan recursively; only direct entries in this folder matching filter conditions are returned.

Retrieving the result of a query

The result of a query is a list of Metadata instances for each of the matching entries. These instances can subsequently be used to perform additional operations, such as reading a file from the filesystem. The API supports retrieving the result either synchronously or asynchronously.

The following example demonstrates how to retrieve the results asynchronously:

Query query = ...;
Drive.DriveApi.query(googleApiClient, query)
        .setResultCallback(new OnChildrenRetrievedCallback() {

    public void onChildrenRetrieved(MetadataBufferResult result) {
        // Iterate over the matching Metadata instances in mdResultSet

Alternatively, the following example demonstrates how to retrieve the results synchronously.

Query query = ...;
MetadataBufferResult mdResultSet = Drive.DriveApi.query(query).await().getMetadataBuffer();
// Iterate over the matching Metadata instances in mdResultSet

Specifying query conditions

As described above, you can use the Filters class to construct query parameters that determine what elements to return. Queries support both boolean and relational operators.

The following example searches for all text files that contain the word "hello":

Query query = new Query.Builder().addFilter(Filters.and(
        Filters.eq(SearchableField.MIME_TYPE, "text/plain"),
        Filters.contains(SearchableField.TITLE, "hello"),
        Filters.eq(customPropertyKey, "world"))).build();

As seen in this example, many instances of Filters can be combined indefinitely to form the desired expression and specify the evaluation order.

The following table lists the metadata fields that can be queried:

Field Type Description
MIME_TYPE String The MIME type of the file
MODIFIED_DATE Date The date the file was last modified
PARENTS DriveId The parent folder(s) of the file or folder
STARRED Boolean Whether the file is starred or not
TITLE String The title of the file
TRASHED Boolean Whether the file is trashed
CustomPropertyKey String The value of the CustomProperty

Add sorting to a query

The SortOrder class can be used to add sorting to queries. Multiple sorting fields can be added to a single query; ties from the first sorting field are broken down by the second sorting field and so on.

The following example adds two sorting fields: the first adds sort ascending by file title, the second adds sort descending by modified date.

SortOrder sortOrder = new SortOrder.Builder()

Query sortedQuery = new Query.Builder()

The following table lists the available sortable fields:

Field Type Description
CREATED_DATE Date The date when the item was created
LAST_VIEWED_BY_ME Date The date this resource was most recently viewed by the user
MODIFIED_BY_ME_DATE Date The date when the item was most recently modified by the user
MODIFIED_DATE Date The date when the item was most recently modified
QUOTA_USED Long The Drive quota used by the file
SHARED_WITH_ME_DATE Date The date this resource was shared with the user
TITLE String The title of the item

Retrieving multiple pages of results

The Google Drive Android API returns paginated results for queries. To retrieve a token for the next page of results, use the getNextPageToken method of the MetadataBuffer class, as shown in this example:

 * Appends the retrieved results to the result buffer.
ResultCallback<MetadataBufferResult> childrenRetrievedResult = new
        ResultCallback<MetadataBufferResult>() {
    public void onResult(MetadataBufferResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Problem while retrieving files");
        mNextPageToken = result.getMetadataBuffer().getNextPageToken();
        mHasMore = mNextPageToken != null;

This page token can then be used to retrieve the next page of results by executing another query as shown in this example:

 * Retrieves results for the next page. For the first run,
 * it retrieves results for the first page.
private void retrieveNextPage() {
    // if there are no more results to retrieve,
    // return silently.
    if (!mHasMore) {
    // retrieve the results for the next page.
    Query query = new Query.Builder()
    Drive.DriveApi.query(getGoogleApiClient(), query).setResultCallback(this);

For a full working sample, the ListFilesActivity in the Google Drive Android API Demo app demonstrates how to list multiple pages of file results.

Send feedback about...

Drive API for Android