本文介绍了检查文件夹是否存在,如果不存在,则创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到对此没有确定的答案,所以我正在寻找一个标准答案来解决这个问题:如何检查文件夹是否存在,如果不存在,请使用Google Drive Android API?。理想地显示使用 ResultCallback 的异步方法以及使用 .await()的同步方法。

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().

PS我知道,但被接受的答案是关注 isTrashed()上已知的滞后错误,并且不清楚代码中实际知道该文件夹存在于哪一点。其他答案似乎已过时。

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.

推荐答案

- 专注于laggy删除状态问题 - 确实提供了用于测试文件夹是否存在的模式。

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

使用同步 .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");
            }
        }

这篇关于检查文件夹是否存在,如果不存在,则创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:24