Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive Android API - Check if folder exists

I'm trying to figure out how to check if a folder exists in Google Drive using the new Google Drive Android API

I've tried the following, thinking that it would either crash or return null if the folder is not found, but it doesn't do that (just as long as it is a valid DriveId, even though the folder has been deleted).

DriveFolder folder = Drive.DriveApi.getFolder(getGoogleApiClient(), driveId));

If i try to create a file the folder I get from the above code, it does not crash either? I'm clearly having a little hard time understanding how this new API works all to together, especially with the very limited tutorials and SO questions out there, and I'm really stuck on this one, so any input will be much appreciated.

Just to clarify my problem: I'm creating a file in a specified Google Drive folder, but if the folder does not exist (has been deleted by user), I want to create it first.

like image 925
Jakob Avatar asked Feb 11 '14 21:02

Jakob


2 Answers

After a lot of research this is the code I ended up with. It works properly, but has an issue: When a folder is trashed in Google Drive it takes some time (hours) before the metadata I can fetch from my app is updated, meaning that this code can first detect if the folder has been trashed a couple of hours later the trashing event actually happened - further information and discussions can be found here.

public class checkFolderActivity extends BaseDemoActivity {

    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);

        DriveId folderId = DriveId.decodeFromString(folderId);
        DriveFolder folder = Drive.DriveApi.getFolder(mGoogleApiClient, folderId);
        folder.getMetadata(mGoogleApiClient).setResultCallback(metadataRetrievedCallback);

    }

     final private ResultCallback<DriveResource.MetadataResult> metadataRetrievedCallback = new
        ResultCallback<DriveResource.MetadataResult>() {
            @Override
            public void onResult(DriveResource.MetadataResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.v(TAG, "Problem while trying to fetch metadata.");
                    return;
                }

                Metadata metadata = result.getMetadata();
                if(metadata.isTrashed()){
                    Log.v(TAG, "Folder is trashed");
                }else{
                    Log.v(TAG, "Folder is not trashed"); 
                }

            }
        };
}
like image 65
Jakob Avatar answered Nov 20 '22 06:11

Jakob


If you're creating a folder based on it's existence status, the 'createTree()' method here does just that.

The following 2 code snippets list files/folders based on arguments passed ( inside a folder, globally, based on MIME type ...). The line with md.getTitle() is the one that you can use to interrogate files/folders.

GoogleApiClient _gac;
void findAll(String title, String mime, DriveFolder fldr) {
  ArrayList<Filter> fltrs = new ArrayList<Filter>();
  fltrs.add(Filters.eq(SearchableField.TRASHED, false));
  if (title != null)  fltrs.add(Filters.eq(SearchableField.TITLE, title));
  if (mime  != null)  fltrs.add(Filters.eq(SearchableField.MIME_TYPE, mime));
  Query qry = new Query.Builder().addFilter(Filters.and(fltrs)).build(); 
  MetadataBufferResult rslt = (fldr == null) ? Drive.DriveApi.query(_gac, qry).await() : 
                                               fldr.queryChildren(_gac, qry).await();
  if (rslt.getStatus().isSuccess()) {
    MetadataBuffer mdb = null;
    try { 
      mdb = rslt.getMetadataBuffer();
      if (mdb == null) return null;
      for (Metadata md : mdb) {
        if ((md == null) || md.isTrashed()) continue; 
        --->>>> md.getTitle()
      }
    } finally { if (mdb != null) mdb.close(); } 
  }
}

void listAll(DriveFolder fldr) {
  MetadataBufferResult rslt = fldr.listChildren(_gac).await();
  if (rslt.getStatus().isSuccess()) {
    MetadataBuffer mdb = null;
    try { 
      mdb = rslt.getMetadataBuffer();
      if (mdb == null) return null;
      for (Metadata md : mdb) {
        if ((md == null) || md.isTrashed()) continue; 
        --->>>> md.getTitle()
      }
    } finally { if (mdb != null) mdb.close(); } 
  }
}

The key is probably checking the "isTrashed()" status. Since 'remove' file on the web only moves it to TRASH. Also, deleting in general (on the website, since there is no 'DELETE' in the API) is a bit flaky. I was testing it for a while, and it may take hours, before the "isTrashed()" status is updated. And manually emptying the trash in Google Drive is also unreliable. See this issue on Github.

There is a bit more talk here, but probably unrelated to your problem.

like image 33
seanpj Avatar answered Nov 20 '22 06:11

seanpj