How to get DriveId of created folder in Google Drive Android API

一曲冷凌霜 提交于 2019-11-26 14:55:27

问题


I have folders on my Google Drive account and i want to add files in specific folder. How can I do it? In example needed DriveId (EXISTING_FOLDER_ID), but I don't know DriveID my folder, I know name only.


回答1:


If you have the metadata for the folder (or a DriveFolder object, from which you can get the metadata), you can just call getDriveId.

If you have the web resource, you can get the DriveId using that resource id using fetchDriveId




回答2:


OK. I experienced the same issue with the Drive demos on Github: https://github.com/googledrive/android-demos

At the bottom of the readme, it says:

If you actually want to run this sample app (though it is mostly provided so you can read the code), you will need to register an OAuth 2.0 client for the package com.google.android.gms.drive.sample.demo with your own debug keys and set any resource IDs to those that you have access to. Resource ID definitions are on:

com.google.android.gms.drive.sample.demo.BaseDemoActivity.EXISTING_FOLDER_ID com.google.android.gms.drive.sample.demo.BaseDemoActivity.EXISTING_FILE_ID

They didn't really specify how exactly one should get and set the resourceID for the EXISTING_FOLDER_ID and EXISTING_FILE_ID constants defined in the BaseDemoActivity file and after reading seanpj comment above, I used his method number 3 to find the DriveIDs so that I can add it to the demo app so that it runs properly.

All one needs to do is to go to Drive on your PC then click on the "link" button at the top.

A portion of the link says "id=XXXXXXXXXXX". Copy that and paste it into the BaseDemoActivity file in the demo app. Do this for both a file and a folder that you create yourself on Drive.

The Demo app should now run successfully.




回答3:


@smokybob. I don't know if it helps with the original question, but per your comment:

Here's a code snippet that should get you folder/file DriveId. The 'title', 'mime' and 'fldr' arguments are optional. In case you pass null for 'fldr', the search is global (within the app's FILE scope), otherwise within the specified folder (not in subfolders, though). It uses the simplest - 'await()' flavor, that has to be run off the UI thread.

Be careful though, the folder / file names are not unique entities in the Google Drive Universe. You can have multiple files / folders with the same name in a single folder !

GoogleApiClent _gac;   // initialized elsewhere

//find files, folders. ANY 'null' ARGUMENT VALUE MEANS 'any'
public void findAll(String title, String mime, DriveFolder fldr) {
  ArrayList<Filter> fltrs = new ArrayList<Filter>();
  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) {
        for (Metadata md : mdb) {
          if (md == null) continue;
          DriveId dId  = md.getDriveId();      // here is the "Drive ID"         
          String title = md.getTitle();
          String mime  = md.getMimeType();
          // ...
        }
      }
    } finally { if (mdb != null) mdb.close(); } 
  }
}

In general terms, you can get the 'DriveId' you need from:

  1. file / folder name as shown in the code above, or
  2. the string identifier you've got from 'encodeToString()'. I use it for caching MYROOT folder id, for instance, or
  3. the string identifier you've got from 'getResourceId()'. This is the string you see in the html address. But since your only scope is FILE, don't count on using it to open something your app did not create.

Both 2 and 3 identifiers are strings, so they may be confused. Identifier 2 is faster when retrieving Drive ID (via decodeFromString()). Identifier 3 is slower to retrieve (via fetchDriveId()), but usefull if you need to take your ID elsewhere (Apps Script, for instance). See also SO 21800257

As far as creation of files / folders is concerned, I have some code on GitHub here. If you look at awaits\MainActivity.java ... buildTree(), you will see the creation of folders / files recursively when building a simple folder tree.




回答4:


you can use query to find folder

Query query = new Query.Builder().addFilter(Filters.and(
                Filters.eq(SearchableField.TITLE, "folder name"),
                Filters.eq(SearchableField.TRASHED, false))).build();

I fount one sample tutorial http://wiki.workassis.com/android-google-drive-api-deleted-folder-still-exists-in-query/ in this tutorial they are calling asynchronously



来源:https://stackoverflow.com/questions/22199537/how-to-get-driveid-of-created-folder-in-google-drive-android-api

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