1

I want to list all files and folders of a directory on Google Drive using Google Drive API. But I am only getting Metadata object of files not of the folders present in the directory. I am using the following code

DriveFolder folder = Drive.DriveApi.getRootFolder(getClient());
folder.listChildren(getClient())
 .setResultCallback(new ResultCallback < DriveApi.MetadataBufferResult > () {@
     Override
     public void onResult(DriveApi.MetadataBufferResult result) {
         if (!result.getStatus().isSuccess()) {
             L.c("Error in listing root folder");
             return;
         }
         MetadataBuffer metadataBuffer = result.getMetadataBuffer();
         Metadata filedata;
         ListItem[] items = new ListItem[metadataBuffer.getCount()];
         L.c("Count :" + metadataBuffer.getCount());
         for (int i = 0; i < metadataBuffer.getCount(); i++) {
             items[i] = new ListItem(metadataBuffer.get(i));
         }
         listAdapter = new ArrayAdapter < ListItem > (ExplorerClass.this, android.R.layout.simple_list_item_1, items);
         mainList.setAdapter(listAdapter);
     }
 });
sv_jan5
  • 1,543
  • 16
  • 42

1 Answers1

3

If the folders or files present in the directory (in your case root) were not created by your app or opened by your app before, you won't be able to see them with your app. This is because you are probably using Drive.FILE scope: https://developers.google.com/drive/web/scopes

Unfortunately, the google drive android API currently supports only drive.file and drive.appfolder scopes. You can use the Google Drive REST API if you need drive.readonly.metadata scope.

Luis
  • 131
  • 3
  • 4
    To add to this (probably correct) answer... Let's say I install your app. Do I really want to allow it to read and delete all of my Drive files? Probably not. Thus Drive implements a range of scopes, the most common being drive.file which only allows an app to see files that that app initially created. Files created outside the app, including folders created in drive.google.com are concealed. – pinoyyid Jul 01 '15 at 15:23
  • 1
    You can also use the file picker, which allows you to list files and folders including those that your app does not yet have access to. https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/PickFolderWithOpenerActivity.java – Arthur Thompson Jul 02 '15 at 13:58
  • Correcting Arthur's answer above. The GDAA's file picker ([intents here](https://github.com/googledrive/android-demos#intents)) does not allow YOU (the app), but allows the CURRENT AUTHORIZED USER of your app to list / view ALL files under her/his jurisdiction – seanpj Sep 29 '15 at 13:45