Search code examples
androidgoogle-drive-apigoogle-play-servicesgoogle-drive-android-api

How do I work with DriveApi.MetadataBufferResult?


I'm using the Drive API and am starting to get very frustrated. I'm trying to use queries to search for files/folders based on their name. This demo has a good example on how to do this: https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/QueryFilesWithAInTitleActivity.java

But I don't want to add the data to a ListView, I just want to extract information from the MetadataBufferResult directly (titles, and IDs so I can work with them). But every example I've looked at only does this ListView thing, and I haven't been able to find a way to extract any information from the MetadataBufferResult or find good documentation on it.

Basically I want to be able to do this (code taken from the demo linked above):

final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback =
            new ResultCallback<DriveApi.MetadataBufferResult>() {
                @Override
                public void onResult(DriveApi.MetadataBufferResult result) {
                    if (!result.getStatus().isSuccess()) {
                        showMessage("Problem while retrieving results");
                        return;
                    }

                    //I haven't been able to find a way to do this
                    result.getDriveID();
                    result.getFileTitle();
                }
            };

Short version: I want to search the root directory of someone's Drive account for the file with the title "x" and then read/write to this file. My problem is that I can't extract data from the MetadataBufferResult object I'm passed and all the examples I've seen output it using a ListView which I don't want to do.


Solution

  • Try this:

    public void onResult(MetadataBufferResult rslt) {
      if (rslt != null && rslt.getStatus().isSuccess()) {
        MetadataBuffer mdb = null;
        try {
          mdb = rslt.getMetadataBuffer();
          if (mdb != null ) for (Metadata md : mdb) {
            if (md == null || !md.isDataValid()) continue;
              md.getTitle(); 
              md.getDriveId();
              md.getAlternateLink();
              //md.get.....();
          }
        } finally { if (mdb != null) mdb.close(); }
      }
    }
    

    type 'md. ...' and you'll get the list of metadata available. The full context can be found here

    Good Luck