Home » Android » Google Drive API – Android – How to obtain a drive file id?

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

Posted by: admin June 15, 2020 Leave a comment


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.

How to&Answers:

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"))

And set a callback function to iterate on the results:

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

    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


The solution i found for this problem was creating the file from the app. Using the class (“CreateFileActivity.java”) from google drive api demo app.

With this class i save the returning Driveid from the new file in a global DriveId variable.

final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
        ResultCallback<DriveFolder.DriveFileResult>() {
            public void onResult(DriveFolder.DriveFileResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("","Error while trying to create the file");
                Log.e("","Created a file with content: " + Id);


Then with this id in another method i call the file and read it (If i want i can edit this file information from Google Drive Web App):

 public void leer(){
       DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),Id);
       file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("Error:","No se puede abrir el archivo o no se encuentra");
                // DriveContents object contains pointers
                // to the actual byte stream
                DriveContents contents = result.getDriveContents();
                BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                } catch (IOException e) {
                String contentsAsString = builder.toString();


I’ve been playing with this stuff a few months back, and still have some code on github. It may be VERY outdated (libver 15 or so), but it may serve as a reference point, and it is (I hope simple). Look here. Pull it down, plug in, step through. Fix what’s not working anymore :-). I’ve abandoned it some time ago.

Be aware of the fact that there are 2 different IDs for Google Drive Android API objects, see SO 22841237.

In general, you usually start with knowing the file/folder name, query GDAA to get a list of objects. Each of them will yield DriveID and ResourceID. DriveID is used in your app to manipulate the objects (does not mean anything outside your Android App and/or device). ResourceID is the string that appears in different forms in URLs and can be used outside your app (web browser for instance…). Look at this wrapper to get some feeling how it works. But again, it’s been a few versions back, so there are no guaranties. Good Luck…