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