Check if folder exists, and create it if not

a 夏天 提交于 2019-12-07 16:05:42

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");
            }
        }
seanpj

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!