我正在使用 Google Drive API V3 使用 Android 应用程序创建文件夹。
创建后,文件 ID 将保存到共享首选项 - 以便稍后使用和附加它。 文件保存为用户信息,而不是应用程序信息,因此用户可以从驱动器中更改它。 文件夹可能已被丢弃或完全删除 - 如果是,我想创建新文件夹。
根据这个explanation about searching files我可以使用 files.list - 但这将返回所有文件。 我只需要特定文件(如果存在)
获取列表时 - 我可以看到 trashed 的值(真/假) 像这样:
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"
}
- 谁能告诉我解决方案以获得值(value) 具体文件?
- setFields() 的结构可以是什么?
非常感谢!!
最佳答案
找到 SetFields() 的结构 - 识别文件是否已删除
File checkFileTrashed = mDriveService.files()
.get(mDriveFolderId)
.setFields("trashed")
.execute();
你可以使用这个reference from Google Drive API
一旦文件被删除 - 你会得到IOException
,
与 "message" : "File not found: <the fileId >
"
您可以捕获它并处理逻辑(创建新文件夹、向用户发送消息等...)
关于Android Drive API V3 - 如何知道文件是否已删除或删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50449393/