[英]How to get DriveId of created folder in Google Drive Android API
I have folders on my Google Drive account and i want to add files in specific folder. 我的Google云端硬盘帐户上有文件夹,我想在特定文件夹中添加文件。 How can I do it? 我该怎么做? In example needed DriveId (EXISTING_FOLDER_ID), but I don't know DriveID my folder, I know name only. 在示例中需要DriveId(EXISTING_FOLDER_ID),但是我不知道DriveID我的文件夹,我只知道名称。
OK. 好。 I experienced the same issue with the Drive demos on Github: https://github.com/googledrive/android-demos 我在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. 如果您确实要运行此示例应用程序(尽管大多数情况下都提供了该示例应用程序,因此您可以阅读代码),则需要在com.google.android.gms.drive.sample.demo包中注册OAuth 2.0客户端。拥有自己的调试键,并将所有资源ID设置为您有权访问的资源。 Resource ID definitions are on: 资源ID定义如下:
com.google.android.gms.drive.sample.demo.BaseDemoActivity.EXISTING_FOLDER_ID com.google.android.gms.drive.sample.demo.BaseDemoActivity.EXISTING_FILE_ID 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. 他们并没有真正指定应如何精确获取和设置BaseDemoActivity文件中定义的EXISTING_FOLDER_ID和EXISTING_FILE_ID常量的resourceID,在阅读了上面的seanpj注释后,我使用了他的方法3来查找DriveID,以便可以将其添加到演示应用程序,使其正常运行。
All one needs to do is to go to Drive on your PC then click on the "link" button at the top. 所有需要做的就是转到PC上的云端硬盘,然后点击顶部的“链接”按钮。
A portion of the link says "id=XXXXXXXXXXX". 链接的一部分显示为“ id = XXXXXXXXXXX”。 Copy that and paste it into the BaseDemoActivity file in the demo app. 复制该文件并将其粘贴到演示应用程序的BaseDemoActivity文件中。 Do this for both a file and a folder that you create yourself on Drive. 对您在云端硬盘上创建的文件和文件夹都执行此操作。
The Demo app should now run successfully. 演示应用程序现在应该可以成功运行。
@smokybob. @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. 这是一个代码段,应该为您提供文件夹/文件DriveId。 The 'title', 'mime' and 'fldr' arguments are optional. 'title','mime'和'fldr'参数是可选的。 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). 如果您为'fldr'传递null ,则搜索是全局的(在应用程序的FILE范围内),否则在指定的文件夹内(但是不在子文件夹中)。 It uses the simplest - 'await()' flavor, that has to be run off the UI thread. 它使用最简单的“ await()”风格,必须从UI线程中运行。
Be careful though, the folder / file names are not unique entities in the Google Drive Universe. 不过请注意,文件夹/文件名在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: 一般而言,您可以从以下位置获取所需的“ DriveId”:
Both 2 and 3 identifiers are strings, so they may be confused. 2和3标识符都是字符串,因此可能会造成混淆。 Identifier 2 is faster when retrieving Drive ID (via decodeFromString()). 检索驱动器ID时(通过decodeFromString()),标识符2更快。 Identifier 3 is slower to retrieve (via fetchDriveId()), but usefull if you need to take your ID elsewhere (Apps Script, for instance). 标识符3的检索速度较慢(通过fetchDriveId()),但是如果需要将ID放在其他位置(例如,Apps Script),则很有用。 See also SO 21800257 另请参阅SO 21800257
As far as creation of files / folders is concerned, I have some code on GitHub here . 至于文件/文件夹的创建,我在GitHub上有一些代码。 If you look at awaits\\MainActivity.java ... buildTree(), you will see the creation of folders / files recursively when building a simple folder tree. 如果查看awaits \\ MainActivity.java ... buildTree(),则在构建简单的文件夹树时将递归地看到文件夹/文件的创建。
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 我在本教程中找到了一个示例教程http://wiki.workassis.com/android-google-drive-api-deleted-folder-still-exists-in-query/
If you have the metadata for the folder (or a DriveFolder object, from which you can get the metadata), you can just call getDriveId . 如果您具有文件夹的元数据(或从中可以获取元数据的DriveFolder对象),则只需调用getDriveId即可 。
If you have the web resource, you can get the DriveId using that resource id using fetchDriveId 如果您拥有Web资源,则可以使用fetchDriveId使用该资源ID 获取DriveId。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.