3

I am trying to get a list of Files in a Folder from Google Drive from my Android app but have been unsuccessful so far. I'm using google-api-drive-v1-rev4-java-1.6.0-beta and google-api-client-1.9.0. I'm also building my code similar to calendar-android-sample and tasks-android-sample from the samples at http://code.google.com/p/google-api-java-client/wiki/Android.

I cant seem to find how to use files() to get a list of folders or even the id of the folder I want. The tasks-android-sample uses '@default' in the get() method to get a list of tasks. What would I use in the get method to get a list of folders first, search for my folder, get the id, then get a list of files in that folder?

AsyncLoadDocs.java: (Note: I'm using getFields() just to see if the Get object contains any metadata, which at this point doesn't.)

package com.mysite.myapp.docs;

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.Drive.Files.Get;
import com.google.api.services.drive.model.File;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Asynchronously load the docs with a progress dialog.
 *
 * @author ms
 */
class AsyncLoadDocs extends AsyncTask<Void, Void, List<String>> {

private static final String TAG = "AsyncLoadDocs";
private final GDocsSync gDocsSync;
private final ProgressDialog dialog;
private final Drive entry = null;
private com.google.api.services.drive.Drive service;

  AsyncLoadDocs(GDocsSync gDocsSync) {
    this.gDocsSync = gDocsSync;
    service = gDocsSync.driveClient;
    dialog = new ProgressDialog(gDocsSync);
  }

  @Override
  protected void onPreExecute() {
    dialog.setMessage("Loading docs...");
    dialog.show();
  }

  @Override
  protected List<String> doInBackground(Void... arg0) {
    try {
      List<String> folderNames = new ArrayList<String>();

      Get get = service.files().get("@default").setProjection("FULL");
      String fields = get.getFields();
      Log.d(TAG, "Fields: " + fields);

      return folderNames;
    } catch (IOException e) {
        gDocsSync.handleGoogleException(e);
      return Collections.singletonList(e.getMessage());
    } finally {
        gDocsSync.onRequestCompleted();
    }
  }

  @Override
  protected void onPostExecute(List<String> result) {
    dialog.dismiss();
  }
}

Any help would be appreciated. Both Calendar and Tasks samples successfully retrieve data from Google using my API key, why doesn't this Drive code?

phoeniix
  • 33
  • 1
  • 4

1 Answers1

5

The Drive API grants access only to two classes of files:

  • Files that a user has created with a given Drive app
  • Files that a user opens with a given Drive app

For security reasons, there's no method to list all files in a user Drive account:

https://developers.google.com/drive/apps_overview#granting_file-level_access

For more options in the Android environment, check out these other answers:

Community
  • 1
  • 1
Claudio Cherubino
  • 14,896
  • 1
  • 35
  • 42
  • Thanks Claudio, That makes sense. For my purposes I dont need access to all files, just my folder. So I guess I'd need to create a folder with my app, then access just that. In the meantime I have begun implementing integration using the docs api. I am running into issues where I get back type/html instead of xml and need to figure out whats going on there. I plan on attending your session (https://developers.google.com/events/io/sessions/gooio2012/705/) on how to integrate Drive with Android at Google IO and maybe we can chat then? – phoeniix Jun 26 '12 at 15:36