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.
4 Answers
@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:
- file / folder name as shown in the code above, or
- the string identifier you've got from 'encodeToString()'. I use it for caching MYROOT folder id, for instance, or
- 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.
-
GitHub Page not found · – Bikesh M May 23 '16 at 07:32
-
I have retired since. There is still a [GDAADemo](https://github.com/seanpjanson/GDAADemo) here, but I have not touched it for a while and GDAA may have evolved. – seanpj May 30 '16 at 19:29
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.

- 19,658
- 27
- 149
- 217
-
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. https://github.com/seanpjanson/GDAADemo – seanpj Jan 15 '15 at 02:12
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

- 8,163
- 6
- 40
- 52
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

- 46,552
- 15
- 93
- 82
-
3I 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? – smokybob Mar 05 '14 at 19:33
-
1In that case you can use the Query API to search for folders with that title: https://developer.android.com/reference/com/google/android/gms/drive/DriveApi.html#query(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.drive.query.Query) – Cheryl Simon Mar 05 '14 at 21:19
-
So can i get folder that was not created via my application, but for example via webinterface [Google Drive](https://drive.google.com) – Werder Mar 06 '14 at 09:48
-
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. https://developer.android.com/reference/com/google/android/gms/drive/DriveApi.html#newOpenFileActivityBuilder() – Cheryl Simon Mar 06 '14 at 15:53
-
@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 Mar 09 '14 at 21:24