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

13,497

Solution 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

Solution 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.

link on google drive

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.

Solution 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.

Solution 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

Share:
13,497
Werder
Author by

Werder

Android Developer

Updated on June 14, 2022

Comments

  • Werder
    Werder almost 2 years

    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.

  • smokybob
    smokybob about 10 years
    I think the boiled down question is: How can I get the DriveId of a folder when I know only the foldername(aka title), and add files to it?
  • Cheryl Simon
    Cheryl Simon about 10 years
    In that case you can use the Query API to search for folders with that title: developer.android.com/reference/com/google/android/gms/drive‌​/…, com.google.android.gms.drive.query.Query)
  • Werder
    Werder about 10 years
    So can i get folder that was not created via my application, but for example via webinterface Google Drive
  • Cheryl Simon
    Cheryl Simon about 10 years
    Your application needs to be authorized to view the folder. You can do this by asking the user to select a folder via the OpenFileActivity where you restrict the mimeType to the folder mimeType. developer.android.com/reference/com/google/android/gms/drive‌​/…
  • seanpj
    seanpj about 10 years
    @Werder Since we're talking about the NEW Google Drive Android API, it is my belief that you can 'have a party' only with files your app created, since only the FILE scope is supported.
  • seanpj
    seanpj over 9 years
    I am repeating myself, but THE CATCH IS that the file in question has to be created by the DEMO app itself. GDAA supports only FILE scope, i.e. only files/folders created by it can be used. In a loosely related matter, I put a piece of test code on Github, that may be useful in addition to the official DEMO (it is much simpler). You are welcome to step through it. github.com/seanpjanson/GDAADemo
  • Bikesh M
    Bikesh M almost 8 years
    GitHub Page not found ·
  • seanpj
    seanpj almost 8 years
    I have retired since. There is still a GDAADemo here, but I have not touched it for a while and GDAA may have evolved.