Google Drive API - Android - How to obtain a drive file id?

Mariano Gonzalez picture Mariano Gonzalez · Nov 24, 2014 · Viewed 9.3k times · Source

I'm trying to develop an android app that can read a xml file stored in my google drive folder, the idea at first is trying to open the file and handle the content.

I've read the Google Drive API docs for android and i reached a point that I'm lost, it's working with file contents.

According to this guide the way to open a file from drive is this:

DriveFile file = ...
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);`

Searching I realized that the complete code (that they not include there is):

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,DriveId.bg(id));
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);`

Well the problem there is that I don't know the file "id". I've tried the id from the web link of google drive, something like this (https://drive.google.com/open?id=1EafJ-T6H4xI9VaUuUO5FMVb9Y30xyr7OHuISQ53avso&authuser=0) but didn´t work.

Answer

Rivero picture Rivero · Nov 24, 2014

You could use the DriveAPI Query method, to retrieve any information about an specific file. you will need to define a query object as the following:

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

And set a callback function to iterate on the results:

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

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

You can find more information on the topic here: https://developers.google.com/drive/android/queries