如何获得特定文件夹的文件列表从谷歌驱动器使用驱动器api。
参考资料 :: https:/developers.google.comdriveandroiddeprecation。
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
是包含备份文件zips的目录,所以我的问题是如何获得一个文件是存储在 AppBackup
目录。
注意:: 我正在使用DRIVE API V3
使用HTTP请求GET https://www.googleapis.com/drive/v2/files/folderId/children
用文件夹Id获取文件夹的子文件夹
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Children;
import com.google.api.services.drive.model.ChildList;
import com.google.api.services.drive.model.ChildReference;
import java.io.IOException;
// ...
public class MyClass {
// ...
/**
* Print files belonging to a folder.
*
* @param service Drive API service instance.
* @param folderId ID of the folder to print files from.
*/
private static void printFilesInFolder(Drive service, String folderId)
throws IOException {
Children.List request = service.children().list(folderId);
do {
try {
ChildList children = request.execute();
for (ChildReference child : children.getItems()) {
System.out.println("File Id: " + child.getId());
}
request.setPageToken(children.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null &&
request.getPageToken().length() > 0);
}
// ...
}
更多 核对
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作为文件夹Id值。
然而,在 迁移到Google Drive API v3 该 Children
和 Parents
收藏品已被删除。使用 files.list
而不是。