如何检查 google 驱动器中是否存在文件夹?

How to Check if the folder is present in trash or not in google drive?

您好,我一直在创建一个应用程序,用于在 gdrive 中上传文件夹和文件,但我面临一个问题,即当文件夹存在于 bin 中时,文件也存储在该文件夹中,我想避免的是检查该文件夹是否在 bin 中,如果是,则从那里删除它。

    public Task<GoogleDriveFileHolder> createFolderIfNotExist(final String folderName, @javax.annotation.Nullable final String parentFolderId) {
        return Tasks.call(mExecutor, new Callable<GoogleDriveFileHolder>() {
            @Override
            public GoogleDriveFileHolder call() throws Exception {
                GoogleDriveFileHolder googleDriveFileHolder = new GoogleDriveFileHolder();
                FileList result = mDriveService.files().list()
                        .setQ("mimeType = '" + DriveFolder.MIME_TYPE + "' and name = '" + folderName + "' and trashed = '" + false + "' ")
                        .setSpaces("drive")
                        .execute();


                if (result.getFiles().size() > 0) {
                    googleDriveFileHolder.setId(result.getFiles().get(0).getId());
                    googleDriveFileHolder.setName(result.getFiles().get(0).getName());
//                googleDriveFileHolder.setModifiedTime(result.getFiles().get(0).getCreatedTime().getValue());
//                googleDriveFileHolder.setSize(result.getFiles().get(0).getSize());

                    googleDriveFileHolder.setId(result.getFiles().get(0).getId());
                    return googleDriveFileHolder;

                } else {

                    List<String> root;
                    if (parentFolderId == null) {
                        root = Collections.singletonList("root");
                    } else {

                        root = Collections.singletonList(parentFolderId);
                    }
                    File metadata = new File()
                            .setParents(root)
                            .setMimeType(DriveFolder.MIME_TYPE)
                            .setName(folderName);

                    File googleFile = mDriveService.files().create(metadata).execute();
                    if (googleFile == null) {
                        throw new IOException("Null result when requesting file creation.");
                    }
                    googleDriveFileHolder.setId(googleFile.getId());
                    return googleDriveFileHolder;
                }
            }
        });
    }

这就是我正在做的,但是当我在 ".setQ("mimeType = '" + DriveFolder.MIME_TYPE + "' and name = '" + folderName + "' 中添加 isTrashed 并被丢弃时,现在出现了错误= '" + false + "' ")"

我认为问题出在您周围的单引号 false。你想提供一个布尔值,而不是一个字符串。

因此,您的问题可能会通过更改您的查询来解决:

.setQ("mimeType = '" + DriveFolder.MIME_TYPE + "' and name = '" + folderName + "' and trashed = '" + false + "' ")

收件人:

.setQ("mimeType = '" + DriveFolder.MIME_TYPE + "' and name = '" + folderName + "' and trashed = false")

参考: