首页 > 解决方案 > 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

标签: androidkotlingoogle-drive-api

解决方案


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中声明ChildrenParents集合已被删除。改为使用files.list

Ref :如何使用 java 在 google drive V3 中搜索文件夹?


推荐阅读