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.txt".

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

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 DriveResourceClient.query as follows:

Task<MetadataBuffer> queryTask = getDriveResourceClient().query(query);

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

A query can alternatively be executed only in a specific folder using the DriveResourceClient.queryChildren, method, as shown by:

        Task<MetadataBuffer> queryTask = getDriveResourceClient().queryChildren(folder, query);
        // END query_children]
                        metadataBuffer -> mResultsAdapter.append(metadataBuffer))
                .addOnFailureListener(this, e -> {
                    Log.e(TAG, "Error retrieving files", e);

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:

                metadataBuffer -> {
                    // Handle results...
                    // ...
        .addOnFailureListener(this, e -> {
            // Handle failure...
            // ...

Alternatively, you can also retrieve the results synchronously. For more information, see Tasks API.

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 starred text files:

Query query =
        new Query.Builder()
                .addFilter(Filters.and(Filters.eq(SearchableField.MIME_TYPE, "text/plain"),
                        Filters.eq(SearchableField.STARRED, true)))

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 sort ascending by file title:

SortOrder sortOrder = new SortOrder.Builder().addSortAscending(SortableField.TITLE).build();
Query query = new Query.Builder().setSortOrder(sortOrder).build();

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

Enviar comentarios sobre…

Drive API for Android
Drive API for Android
¿Necesitas ayuda? Visita nuestra página de asistencia.