get metadata of all files and folders in Google Drive

假如想象 提交于 2019-12-19 12:53:29

问题


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.

回答1:


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.



来源:https://stackoverflow.com/questions/31453350/get-metadata-of-all-files-and-folders-in-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!