1

I notice that there is not a conclusive answer to this on SO, so I am looking for a canonical answer to the question "How to check if a folder exists, and create it if it does not, using the Google Drive Android API?". Ideally showing examples of both the asynchronous approach using ResultCallback, and the synchronous approach using .await().

P.S. I am aware of this question with the same title, but the accepted answer is focussing on the known bug of lag on isTrashed(), and is not clear about at which point in the code you actually know the folder exists. Other answers seem out of date.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
Mike Ounsworth
  • 2,444
  • 21
  • 28

2 Answers2

9

This question - while focussing on the laggy deletion status issue - does provide a pattern for testing if a folder exists.

Using an asynchronous callback:

Query query = new Query.Builder()
    .addFilter(Filters.and(Filters.eq(
            SearchableField.TITLE, "MyFolder"),
            Filters.eq(SearchableField.TRASHED, false)))
    .build();
    Drive.DriveApi.query(getGoogleApiClient(), query)
        .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() {
    @Override
    public void onResult(DriveApi.MetadataBufferResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Cannot create folder in the root.");
        } else {
            boolean isFound = false;
            for(Metadata m : result.getMetadataBuffer()) {
                if (m.getTitle().equals("MyFolder")) {
                    showMessage("Folder exists");
                    isFound = true;
                    break;
                }
            }
            if(!isFound) {
                showMessage("Folder not found; creating it.");
                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("MyFolder")
                    .build();
                Drive.DriveApi.getRootFolder(getGoogleApiClient())
                        .createFolder(getGoogleApiClient(), changeSet)
                        .setResultCallback(new ResultCallback<DriveFolder.DriveFolderResult>() {
                    @Override
                    public void onResult(DriveFolder.DriveFolderResult result) {
                        if (!result.getStatus().isSuccess()) {
                            showMessage("Error while trying to create the folder");
                        } else {
                            showMessage("Created a folder");
                        }
                    }
                });
            }
        }
    }
});

Using a synchronous .await()

Query query = new Query.Builder()
    .addFilter(Filters.and(Filters.eq(
            SearchableField.TITLE, "MyFolder"),
            Filters.eq(SearchableField.TRASHED, false)))
    .build();

    DriveApi.MetadataBufferResult result = Drive.DriveApi.query(getGoogleApiClient(), query)
        .await();

    if (!result.getStatus().isSuccess()) {
        showMessage("Cannot create folder in the root.");
    } else {
        boolean isFound = false;
        for(Metadata m : result.getMetadataBuffer()) {
            if (m.getTitle().equals("MyFolder")) {
                showMessage("Folder exists");
                isFound = true;
                break;
            }
        }
        if(!isFound) {
            showMessage("Folder not found; creating it.");
            MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("MyFolder")
                    .build();

            Drive.DriveApi.getRootFolder(getGoogleApiClient())
                        .createFolder(googleApiClient, changeSet).await();

            if (!result.getStatus().isSuccess()) {
                showMessage("Error while trying to create the folder");
            } else {
                showMessage("Created a folder");
            }
        }
Mike Ounsworth
  • 2,444
  • 21
  • 28
  • Can you tell me how to update file inside a folder if already exits? – Atula Jun 23 '16 at 13:35
  • @Atula Sorry, I haven't done any Android in 6 months and I really don't remember how it works :( – Mike Ounsworth Jun 23 '16 at 14:10
  • I m using your async code and it was working fine earlier but now it started giving false folder existence sometimes, any idea to fix? – Panache Jan 10 '17 at 17:31
  • @Panache I don't know what you mean by "giving false folder existence" can you explain more? Also, I don't really understand this is code because I copied it from the liked Answer, plus it's been almost a year since I've done Android dev, so I might not be able to answer anyway. – Mike Ounsworth Jan 10 '17 at 19:07
  • @Panache If the folder is in the trash, the folder exists – kike0kike Dec 05 '17 at 17:16
0

Your quote:

... is not clear about at which point in the code you actually know the folder exists

In the REST Api, you wait for a response from the 'ecexute()' method. Straightforward, and you can time the response out. You get folder/file id (ResourceId) and you know it exists in the Drive.

In GDAA, look at this answer. Again, when you get completion notification with a valid ResourceId, you know that the folder/file is 'up-there'.

Good Luck

Community
  • 1
  • 1
seanpj
  • 6,735
  • 2
  • 33
  • 54