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.
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");
}
}
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
来源:https://stackoverflow.com/questions/33851649/check-if-folder-exists-and-create-it-if-not