获取Google云端硬盘中所有文件和文件夹的元数据 [英] get metadata of all files and folders in Google Drive

查看:103
本文介绍了获取Google云端硬盘中所有文件和文件夹的元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个Android应用程序中实施Google云端硬盘。我能够进行身份验证。



在这个方法中,我想列出所有文件和文件夹的元数据。

  Drive.DriveApi.fetchDriveId(DemoUtil.getGoogleApiClient(),< drive_id>)
.setResultCallback(idCallback)

但这不起作用。它需要驱动器ID,从哪里我会得到这个驱动器ID?
我可以获取Google云端硬盘所有文件和文件夹的元数据列表吗?


  • 我使用播放服务的API。 //developers.google.com/drive/android/introrel =nofollow> GDAA ('Play services'的API)只会为您提供由您的Android App创建的文件,文件夹,因为它支持只有'SCOPE_FILE'范围。如果这是好的,你可以使用这样的构造:

      ArrayList< ContentValues> contvals = search(null,null,null); 
    ...
    private static GoogleApiClient mGAC;
    / ********************************************* *****
    *在GOODrive中查找文件/文件夹
    * @param prnId父ID(可选),空搜索全驱,根搜索驱动器根目录
    * @参数titl文件/文件夹名称(可选)
    * @param mime文件/文件夹mime类型(可选)
    * @返回找到对象的数组列表
    * /
    static ArrayList< ContentValues> search(String prnId,String titl,String mime){
    ArrayList< ContentValues> gfs = new ArrayList<>();
    if(mGAC!= null& mGAC.isConnected())try {
    //添加查询条件,构建查询
    ArrayList< Filter> fltrs = new ArrayList<>();
    if(prnId!= null){
    fltrs.add(Filters.in(SearchableField.PARENTS,
    prnId.equalsIgnoreCase(root)?
    Drive.DriveApi.getRootFolder (mGAC).getDriveId():DriveId.decodeFromString(prnId)));
    }
    if(titl!= null)fltrs.add(Filters.eq(SearchableField.TITLE,titl));
    if(mime!= null)fltrs.add(Filters.eq(SearchableField.MIME_TYPE,mime));
    Query qry = new Query.Builder()。addFilter(Filters.and(fltrs))。build();

    //启动查询
    MetadataBufferResult rslt = Drive.DriveApi.query(mGAC,qry).await();
    if(rslt.getStatus()。isSuccess()){
    MetadataBuffer mdb = null;
    尝试{
    mdb = rslt.getMetadataBuffer();
    for(Metadata md:mdb){
    if(md == null ||!md.isDataValid()|| md.isTrashed())continue;
    gfs.add(UT.newCVs(md.getTitle(),md.getDriveId()。encodeToString()));
    }
    } finally {if(mdb!= null)mdb.close(); }
    }
    } catch(Exception e){}
    return gfs;
    }

    只需修改您的ContentValues 大小 您想要的元数据。摘录摘自此演示,您可以在其中找到未解析的上下文..



    如果您需要所有文件/文件夹,而不考虑创建它们的应用程序,则必须使用 REST API与'DriveScopes.DRIVE'范围相同,并且相同的代码片段如下所示:

     的ArrayList< ContentValues> contvals = search(null,null,null); 
    ...

    private static Drive mGOOSvc;
    / ********************************************* ******************************
    *在GOODrive中查找文件/文件夹
    * @param prnId父ID(可选),空搜索全驱动器,根搜索驱动器根目录
    * @参数titl文件/文件夹名称(可选)
    * @param mime文件/文件夹mime类型(可选)
    *返回找到的对象的数组列表
    * /
    static ArrayList< ContentValues> search(String prnId,String titl,String mime){
    ArrayList< ContentValues> gfs = new ArrayList<>();
    if(mGOOSvc!= null& mConnected)try {
    //添加查询条件,构建查询
    字符串qryClause ='mein owners and;
    if(prnId!= null)qryClause + ='+ prnId +'在父母和;
    if(titl!= null)qryClause + =title ='+ titl +'and;
    if(mime!= null)qryClause + =mimeType ='+ mime +'and;
    qryClause = qryClause.substring(0,qryClause.length() - and.length());
    Drive.Files.List qry = mGOOSvc.files().list().setQ(qryClause)
    .setFields(items(id,mimeType,labels / trashed,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;
    gfs.add(UT.newCVs(gFl.getTitle(),gFl.getId()));
    } // else UT.lg(failed+ gFl.getTitle());
    npTok = gLst.getNextPageToken();
    qry.setPageToken(npTok);
    }
    } while(npTok!= null&& npTok.length()> 0); //UT.lg(\"found+ vlss.size());
    } catch(Exception e){UT.le(e); }
    返回gfs;
    }

    取自 here

    祝您好运。


    I am implementing Google Drive in one of my Android application. I am able to authenticate.

    Here in this method I want to list down metadata of all the files and folders.

    Drive.DriveApi.fetchDriveId(DemoUtil.getGoogleApiClient(), <drive_id>)
                .setResultCallback(idCallback)
    

    But this is not working. It requires drive id, from where I'll get this drive id? Can I get the metadata listing of all files and folder of Google Drive?

    • I am using API of play services.

    解决方案

    First, be aware of the fact, that using GDAA ('API of play services') will give you only the files, folders created by your Android App, since it supports only the 'SCOPE_FILE' scope. If this is OK, you may use a construct like this:

    ArrayList<ContentValues> contvals = search(null, null, null);
    ...
    private static GoogleApiClient mGAC;
    /**************************************************
     * find file/folder in GOODrive
     * @param prnId   parent ID (optional), null searches full drive, "root" searches Drive root
     * @param titl    file/folder name (optional)
     * @param mime    file/folder mime type (optional)
     * @return        arraylist of found objects
     */
    static ArrayList<ContentValues> search(String prnId, String titl, String mime) {
      ArrayList<ContentValues> gfs = new ArrayList<>();
      if (mGAC != null && mGAC.isConnected()) try {
        // add query conditions, build query
        ArrayList<Filter> fltrs = new ArrayList<>();
        if (prnId != null){
          fltrs.add(Filters.in(SearchableField.PARENTS,
          prnId.equalsIgnoreCase("root") ?
            Drive.DriveApi.getRootFolder(mGAC).getDriveId() : DriveId.decodeFromString(prnId)));
        }
        if (titl != null) fltrs.add(Filters.eq(SearchableField.TITLE, titl));
        if (mime != null) fltrs.add(Filters.eq(SearchableField.MIME_TYPE, mime));
        Query qry = new Query.Builder().addFilter(Filters.and(fltrs)).build();
    
        // fire the query
        MetadataBufferResult rslt = Drive.DriveApi.query(mGAC, qry).await();
        if (rslt.getStatus().isSuccess()) {
          MetadataBuffer mdb = null;
          try {
            mdb = rslt.getMetadataBuffer();
            for (Metadata md : mdb) {
              if (md == null || !md.isDataValid() || md.isTrashed()) continue;
              gfs.add(UT.newCVs(md.getTitle(), md.getDriveId().encodeToString()));
            }
          } finally { if (mdb != null) mdb.close(); }
        }
      } catch (Exception e) { }
      return gfs;
    }
    

    just modify your ContentValues contvals, to hold the metadata you desire. Snippet is taken from this demo, where you can find unresolved context..

    If you need all files/folders regardless of the app that created them, you have to go for the REST API with 'DriveScopes.DRIVE' scope, and the same code snippet would look like this:

    ArrayList<ContentValues> contvals = search(null, null, null);
    ...
    
    private static Drive mGOOSvc;
    /***************************************************************************
     * find file/folder in GOODrive
     * @param prnId   parent ID (optional), null searches full drive, "root" searches Drive root
     * @param titl    file/folder name (optional)
     * @param mime    file/folder mime type (optional)
     * @return        arraylist of found objects
     */
    static ArrayList<ContentValues> search(String prnId, String titl, String mime) {
      ArrayList<ContentValues> gfs = new ArrayList<>();
      if (mGOOSvc != null && mConnected) try {
        // add query conditions, build query
        String qryClause = "'me' in owners and ";
        if (prnId != null) qryClause += "'" + prnId + "' in parents and ";
        if (titl != null) qryClause += "title = '" + titl + "' and ";
        if (mime != null) qryClause += "mimeType = '" + mime + "' and ";
        qryClause = qryClause.substring(0, qryClause.length() - " and ".length());
        Drive.Files.List qry = mGOOSvc.files().list().setQ(qryClause)
        .setFields("items(id,mimeType,labels/trashed,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;
              gfs.add( UT.newCVs(gFl.getTitle(),gFl.getId()));
            }                                              //else UT.lg("failed " + gFl.getTitle());
            npTok = gLst.getNextPageToken();
            qry.setPageToken(npTok);
          }
        } while (npTok != null && npTok.length() > 0);            //UT.lg("found " + vlss.size());
      } catch (Exception e) { UT.le(e); }
      return gfs;
    }
    

    taken from here.

    Good Luck.

    这篇关于获取Google云端硬盘中所有文件和文件夹的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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