android - 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"
}
- 谁能指导我获取特定文件的价值的解决方案?
- setFields() 的结构是什么?
非常感谢!!
解决方案
找到SetFields () 的结构 - 确定文件是否已被丢弃
File checkFileTrashed = mDriveService.files()
.get(mDriveFolderId)
.setFields("trashed")
.execute();
一旦文件被删除- 你会得到IOException
, 与"message" : "File not found: <the fileId >
"
你可以抓住它并处理逻辑(创建新文件夹,向用户发送消息等......)
推荐阅读
- if-statement - 将 SYSTEMCTL 状态的 grep 搜索结果用于 IF CONDITION
- sql - 如何将一列的平均值分成多列?
- django - 如果记录存在于 1:1 中,则将 Createview 重定向到 Updateview
- javascript - 如何制作表单(两个输入)将我带到另一个页面,该页面的选择取决于输入
- c++ - 模板类赋值运算符
- r - 使用 purrr 过滤列表中的值的嵌套数据框(列表列)
- c# - 等待具有任务的功能完成,然后再继续
- symfony - Symfony4 如何将参数自动连接到 UserPasswordEncoderCommand 构造函数?
- javascript - 如何将 github OAuth 数据发送给客户端?
- regex - 使用 Perl 正则表达式替换引号封装字符串中的引号