如何使用 Java 检测文件是否存在于 Google Drive 中(并且未被删除)?

woo*_*666 5 java google-drive-api

非常不言自明的标题。我正在使用适用于 Java 的 Google Drive Client Api。我目前拥有的内容如下:

File f = mService.files.get(fileId).execute();
Run Code Online (Sandbox Code Playgroud)

但是,我找不到用于File检查文件是否已被删除的属性。File.getExplicitlyTrashed()为我提供了已删除和未删除文件的 null 值。

woo*_*666 3

trashed属性隐藏在File.Labels类内部,您可以从 获取该属性File.getLabels()。一个有效的例子是:

public boolean validFileId(String id) {
    try {
        File f = mService.files().get(id).execute();
        return !f.getLabels().getTrashed();
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("bad id: " + id);
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)