android - Google Drive 从文件夹中获取文件
问题描述
如何使用驱动器 api 从谷歌驱动器获取特定文件夹的文件列表
参考 :: https://developers.google.com/drive/android/deprecation
FileList
public Task<FileList> fetchLatestInformation(@NotNull String backupDir) {
return Tasks.call(mExecutor, () -> {
Drive.Files.List list = drive.files().list().setFields("nextPageToken, files(id, name, modifiedTime, size, createdTime, parents, appProperties)");
StringBuilder sb = new StringBuilder();
sb.append("mimeType = 'application/vnd.google-apps.folder' and name = '");
sb.append(backupDir);
sb.append("'");
list.setQ(sb.toString());
list.setPageSize(1000);
list.setOrderBy("modifiedTime");
return list.execute();
});
}
ActivityCode
googleDrive.fetchLatestInformation(backupDir)
.addOnSuccessListener {
var files = it.files
files.reverse()
files.forEach {
Log.e(TAG,"file :: ${it?.name}")
}
var file = files.firstOrNull()
if (file != null) {
var time = file.modifiedTime
var date = Date(time.value)
var simpleDateFormat = SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa")
Log.e(TAG,"lastBackup name :: ${file.name}")
Log.e(TAG,"lastBackup date :: ${simpleDateFormat.format(date)}")
Log.e(TAG,"lastBackup size :: ${file.getSize()?.formatFileSize}")
}
}
.addOnFailureListener {
it.printStackTrace()
}
LogResult
E/BackupActivity: file :: AppBackup
E/BackupActivity: lastBackup name :: AppBackup
E/BackupActivity: lastBackup date :: Tue, 19 May 2020 10:44 PM
lastBackup size :: null
但是AppBackup
是包含备份文件压缩包的目录,所以我的问题是如何获取存储在AppBackup
目录中的文件。
注意 ::我正在使用驱动 API V3
解决方案
public Task<FileList> fetchLatestInformation(@NotNull String parentFolderId) {
return Tasks.call(mExecutor, () -> {
Drive.Files.List list = drive.files().list().setFields("nextPageToken, files(id, name, modifiedTime, size, createdTime, parents, appProperties)");
StringBuilder sb = new StringBuilder();
sb.append("'");
sb.append(parentFolderId);
sb.append("'");
sb.append(" in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false");
list.setQ(sb.toString());
list.setPageSize(1000);
list.setOrderBy("modifiedTime");
return list.execute();
});
}
Children.List
在 V3 中被删除,所以我们想使用 list() 我们可以使用查询'parentFolderId' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false
在 v2 中,Children: list
用于列出文件夹的子文件夹和列出根文件夹的所有子文件夹,使用别名 root 作为 folderId 值。
但是,在Migrate to Google Drive API v3中声明Children
和Parents
集合已被删除。改为使用files.list
。
推荐阅读
- ubuntu - 如何在不使用 Auditd 或默认 Ubuntu 安装的情况下在 Ubuntu 中进行流程审核?
- git - Git中的版本编号文件夹是不好的做法吗?
- r - 快速单击操作按钮时,observeEvent 和 update*Input 之间的竞争
- c# - C# await 关键字是否会导致函数调用阻塞?
- r - How to multiply variables inplace in a dataframe of a long format?
- python - 如何使用 geomdl 或其他库向样条曲线添加边界约束?
- javascript - js 将表单数据从 https 发布到 http
- couchbase - 从 Couchbase 中的动态数组字段中获取数据
- python - 如何编写可以多次启动的单个进程类?
- mysql - Azure MySQL 上的 Sysbench - SQL 错误,errno = 2013,状态 = 'HY000':查询期间丢失与 MySQL 服务器的连接