2

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.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Zarwan
  • 5,537
  • 4
  • 30
  • 48
  • just to clarify, you're using GDAA, not the REST API. So you're looking in the Drive filestore on the device? – pinoyyid Aug 22 '15 at 15:57
  • Idk what GDAA stands for, but I'm not using the REST API. I'm using the Drive API that comes in the Google play services library that I access using my GoogleApiClient. @pinoyyid – Zarwan Aug 22 '15 at 17:28
  • [GDAA](https://developers.google.com/drive/android/intro) = Google Drive Android Api, [REST](https://developers.google.com/drive/web/about-sdk) = RESTful Api. Sorry, it was called 'Google Drive Android Api' for some years, they renamed it recently to 'Drive API for Android' – seanpj Aug 22 '15 at 19:21
  • If you look [here(GDAA)](https://github.com/seanpjanson/GDAADemo) and [here(REST)](https://github.com/seanpjanson/RESTDemo), you'll see the difference. Actually, REST sits underneath GDAA, so it has richer interface. GDAA, on the other hand handles the network, off-line caching, ... for you. Mixed blessing, though... – seanpj Aug 22 '15 at 19:30

1 Answers1

7

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

seanpj
  • 6,735
  • 2
  • 33
  • 54
  • Hi, whenever I try to do mdb.get(0) or any index I get a java.lang.IllegalStateException (I'm using the evaluate code fragment feature in IntelliJ to try it out) – Zarwan Aug 22 '15 at 21:59
  • I'm not doing GDAA these days, I just smacked together piece of code from the GitHub demo referred above. It would take me way too long to revive it and run the tests I believe you can manage quicker than I would. – seanpj Aug 23 '15 at 00:40
  • Would giving you the setup code help? I tried debugging it but couldn't get anywhere. I'm not sure what the problem is. – Zarwan Aug 23 '15 at 00:49
  • My problem seems to be that my result.getMetadataBuffer is empty, i.e. there are noe Metadata objects in it that I can access. I'm not sure why though, the query I am making should return some results. – Zarwan Aug 23 '15 at 01:24
  • I'll be off-line for a few days, you may be better off pulling the [GDAADemo](https://github.com/seanpjanson/GDAADemo) and start from there. After all, it is a full CRUD, so you should be able to build from it. You may replace the 'awaits' there with 'result callbacks' to avoid using AsyncTasks.. – seanpj Aug 23 '15 at 01:27
  • I'll look into it, thanks. If I get stuck again I'll just make another question, I think this one has been answered. – Zarwan Aug 23 '15 at 01:29
  • Your problem may also be related to the fact that GDAA supports only FILE scope, i.e. it does not see files created outside of your Android app. – seanpj Aug 23 '15 at 01:31
  • 2
    Wait, so it only sees the files created by the app itself? I can't just get existing files in Drive? – Zarwan Aug 23 '15 at 01:32
  • 1
    That's why I dropped GDAA for REST. REST supports DRIVE scope, i.e. it sees everything (the FILE/DRIVE scope names may not be precise here, look at the 'init()' method [here](https://github.com/seanpjanson/GDAADemo/blob/master/app/src/main/java/com/spjanson/gdaademo/GDAA.java) and [here](https://github.com/seanpjanson/RESTDemo/blob/master/app/src/main/java/com/spjanson/restdemo/REST.java)) – seanpj Aug 23 '15 at 01:38
  • But there should be a way to increase your scope using the Drive API for Android right? Or is the only alternative really REST? – Zarwan Aug 23 '15 at 01:44
  • Scroll to 'Intents' [here](https://github.com/googledrive/android-demos) (Create a file with creator activity, Pick a file with opener activity, Pick a folder with opener activity). Your GDAA app actually gives control to the user. The app itself can not access anything outside of it's jurisdiction. – seanpj Aug 23 '15 at 01:51