Google Drive Android API - Check if folder exists

前端 未结 4 1996
傲寒
傲寒 2020-12-03 14:00

I\'m trying to figure out how to check if a folder exists in Google Drive using the new Google Drive Android API

I\'ve tried the following, thinking that it would ei

4条回答
  •  一向
    一向 (楼主)
    2020-12-03 14:35

    If you're creating a folder based on it's existence status, the 'createTree()' method here does just that.

    The following 2 code snippets list files/folders based on arguments passed ( inside a folder, globally, based on MIME type ...). The line with md.getTitle() is the one that you can use to interrogate files/folders.

    GoogleApiClient _gac;
    void findAll(String title, String mime, DriveFolder fldr) {
      ArrayList fltrs = new ArrayList();
      fltrs.add(Filters.eq(SearchableField.TRASHED, false));
      if (title != null)  fltrs.add(Filters.eq(SearchableField.TITLE, title));
      if (mime  != null)  fltrs.add(Filters.eq(SearchableField.MIME_TYPE, mime));
      Query qry = new Query.Builder().addFilter(Filters.and(fltrs)).build(); 
      MetadataBufferResult rslt = (fldr == null) ? Drive.DriveApi.query(_gac, qry).await() : 
                                                   fldr.queryChildren(_gac, qry).await();
      if (rslt.getStatus().isSuccess()) {
        MetadataBuffer mdb = null;
        try { 
          mdb = rslt.getMetadataBuffer();
          if (mdb == null) return null;
          for (Metadata md : mdb) {
            if ((md == null) || md.isTrashed()) continue; 
            --->>>> md.getTitle()
          }
        } finally { if (mdb != null) mdb.close(); } 
      }
    }
    
    void listAll(DriveFolder fldr) {
      MetadataBufferResult rslt = fldr.listChildren(_gac).await();
      if (rslt.getStatus().isSuccess()) {
        MetadataBuffer mdb = null;
        try { 
          mdb = rslt.getMetadataBuffer();
          if (mdb == null) return null;
          for (Metadata md : mdb) {
            if ((md == null) || md.isTrashed()) continue; 
            --->>>> md.getTitle()
          }
        } finally { if (mdb != null) mdb.close(); } 
      }
    }
    

    The key is probably checking the "isTrashed()" status. Since 'remove' file on the web only moves it to TRASH. Also, deleting in general (on the website, since there is no 'DELETE' in the API) is a bit flaky. I was testing it for a while, and it may take hours, before the "isTrashed()" status is updated. And manually emptying the trash in Google Drive is also unreliable. See this issue on Github.

    There is a bit more talk here, but probably unrelated to your problem.

提交回复
热议问题