Search code examples
androidgoogle-drive-android-api

Query fails Google Drive Android


I'm using this code:

GoogleApiClient client = new GoogleApiClient.Builder(this).addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .build();
Query query = new Query.Builder().addFilter(Filters.and(Filters.and(Filters.and(Filters.eq
                    (SearchableField.TITLE, getString(R.string.app_name)), Filters
                    .eq(SearchableField.TRASHED, false)), Filters.eq(SearchableField.PARENTS, Collections
                    .singletonList(Drive.DriveApi
                    .getRootFolder(client)
                    .getDriveId()))), Filters.eq(SearchableField.MIME_TYPE, GoogleDriveClient.FOLDER_MIME))).build();
            DriveApi.MetadataBufferResult result = Drive.DriveApi.query(client, query).await(TIMEOUT, TimeUnit.SECONDS);
            if (!result.getStatus().isSuccess()) {
                client.disconnect();
                return;
            }

There is always an error: Status{statusCode=INTERNAL_ERROR, resolution=null}

I enabled the Drive Api in Google Console, I checked again my SHA1 fingerprint and other operations are working. Is it allowed to perform a query?


Solution

  • Here is a documentation for 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.

    Note: The Android Drive API only works with the https://www.googleapis.com/auth/drive.file scope. This means that only files which a user has opened or created with your application can be matched by a query.

    Here is an example to build 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"))
            .build();
    

    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= ...;
    folder.query(query);
    

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