首页 > 解决方案 > Android Drive API V3 - 如何知道文件是否被删除或删除?

问题描述

我正在使用Google Drive API V3使用 android 应用程序创建文件夹。

创建后,文件 ID 将保存到共享首选项 - 以供以后使用和附加。文件保存为用户信息,而不是应用程序信息,因此用户可以从驱动器更改它。文件夹可能被彻底删除或删除 - 如果是,我想创建新文件夹。

根据这个关于搜索文件的解释,我可以使用 files.list - 但这将返回所有文件。我只需要特定的文件(如果存在)

获取列表时-我可以像这样看到已丢弃(真/假)的值:

List<String> fileInfo = new ArrayList<String>();
FileList result = mDriveServiceForTrash.files().list()
         .setPageSize(100)
         .setFields("nextPageToken, files(id, name,trashed)")
         .execute();
List<File> files = result.getFiles();

if (files != null) {
    for (File file : files) {
        fileInfo.add(String.format("%s (%s) trashed =%s\n",
            file.getName(), file.getId(),file.getTrashed()));
    }
}

在我的代码中 -mDriveFolderId是创建后具有文件夹 ID 的字符串 - 取自"file.getId()".

我试过用 -

File checkFileTrashed = mDriveService.files()
    .get(mDriveFolderId)
    .setFields("files(trashed)")
    .execute();

这将返回异常:

error = 400 Bad Request {
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "fields",
    "locationType" : "parameter",
    "message" : "Invalid field selection files",
    "reason" : "invalidParameter"
  } ],
  "message" : "Invalid field selection files"
}
  1. 谁能指导我获取特定文件的价值的解决方案?
  2. setFields() 的结构是什么?

非常感谢!!

标签: androidgoogle-drive-apigoogle-drive-android-api

解决方案


找到SetFields () 的结构 - 确定文件是否已被丢弃

File checkFileTrashed = mDriveService.files()
    .get(mDriveFolderId)
    .setFields("trashed")
    .execute();

您可以从 Google Drive API 使用此参考

一旦文件被删除- 你会得到IOException, 与"message" : "File not found: <the fileId >"

你可以抓住它并处理逻辑(创建新文件夹,向用户发送消息等......)


推荐阅读