Google Drive API - 获取包括文件夹的文件列表 [英] Google Drive API - get list of files including folders

查看:223
本文介绍了Google Drive API - 获取包括文件夹的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用

我需要列出用户驱动云中的所有图片。 / filesrel =nofollow> https://www.googleapis.com/drive/v2/files ,并使用正确的过滤器查询所有图像。我需要将结果分组到文件夹中的应用程序中。我知道一个文件可以有多个父母,没关系。



我希望避免通过一次调用来创建和调用文件夹 https://developers.google.com/drive/v2/reference/files/get 使用第一次调用中的父ID。



是否有一种网络友好的方式来获取所有包含文件夹的文件?



编辑



一个简单的解决方案是将所有带有ids的文件夹放入一个查询和查找此结果中的文件夹名称。也许这在某种程度上是可能的?

解决方案

正如你在上面的评论中回答自己的(但你不能匹配名字,你必须匹配ID;名称不唯一)。



第1步:一次获取所有文件夹(分页结果,过滤不需要的字段,跳过垃圾箱):

  private static Drive mGOOSvc; 
....
static ArrayList< ContentValues> getFolders(){
ArrayList< ContentValues> cvs = new ArrayList<>();
if(mGOOSvc!= null)try {
Drive.Files.List qry = mGOOSvc.files()。list()
.setQ(mimeType ='application / vnd.google- apps.folder')
.setFields(items(id,labels / trashed,parents / id,title),nextPageToken);
String npTok = null;
if(qry!= null)do {
FileList gLst = qry.execute();如果(gLst!= null){
for(File gFl:gLst.getItems()){
if(gFl.getLabels()。getTrashed())continue;
for(ParentReference parent:gFl.getParents())
cvs.add(newContentValues(gFl.getTitle(),gFl.getId(),parent.getId()));
}
npTok = gLst.getNextPageToken();
qry.setPageToken(npTok);
}
} while(npTok!= null&& npTok.length()> 0);
} catch(Exception e){/ *处理异常* /}
return cvs;

第2步:解析生成的ArrayList树结构(匹配ParentIds,处理多个父母)



第3步:对MIME类型为image / jpeg ,image / png,...任何img mimetype(只是修改上面的代码来获取文件)并再次解析。



当然'execute )'方法会产生应该按照指示处理的异常此处 a>。

...您可以采用'不太友善'的方法迭代文件夹树, 'testTree()'方法 here 。如果你不知道你的树结构有多深,递归是必要的。



好运

I need to list all images in a users drive cloud.

I use https://www.googleapis.com/drive/v2/files with the correct filter to query all images. I need to group the result in my app in folders. I know a file can have multiple parents, that's fine.

I would like to avoid making and calls (for every single file a single call) to get a files folder via https://developers.google.com/drive/v2/reference/files/get using the parent id from the first call.

Is there a network friendly way to get all files inclusive there folders?

EDIT

A simple solution would be to get all folders with ids in one query and lookup folder names in this result. Maybe that's somehow possible?

解决方案

As you answered yourself in the comments above (but you can't match names, you have to match IDs; names aren't unique).

Step 1: get all your folders in one shot (paging results, filtering unneeded fields, skipping the trashed ones):

  private static Drive mGOOSvc;
  ....
  static ArrayList<ContentValues> getFolders() {
    ArrayList<ContentValues> cvs = new ArrayList<>();
    if (mGOOSvc != null) try {
      Drive.Files.List qry = mGOOSvc.files().list()
        .setQ("mimeType = 'application/vnd.google-apps.folder'")
        .setFields("items(id,labels/trashed,parents/id,title),nextPageToken");
      String npTok = null;
      if (qry != null) do {
        FileList gLst = qry.execute();
        if (gLst != null) {
          for (File gFl : gLst.getItems()) {
            if (gFl.getLabels().getTrashed()) continue;
            for (ParentReference parent : gFl.getParents())
              cvs.add(newContentValues(gFl.getTitle(), gFl.getId(), parent.getId()));
          }
          npTok = gLst.getNextPageToken();
          qry.setPageToken(npTok);
        }
      } while (npTok != null && npTok.length() > 0);
    } catch (Exception e) { /* handle Exceptions */ }
    return cvs;
  }

Step 2: Parse the resulting ArrayList to build the tree structure (match ParentIds, handle multiple parents)

Step 3: Do the same for files with mime type ""image/jpeg", "image/png", ... "whatever img mimetype" (just modify the code above to get files) and parse again.

Of course the 'execute()' method will produce exceptions that should be handled as pointed out here.

... and you can take the 'not so network friendly' approach of iterating down the folder tree as seen in the 'testTree()' method here. Recursion is necessary if you have no knowledge how deep your tree structure is.

Good Luck

这篇关于Google Drive API - 获取包括文件夹的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆