简体   繁体   English

Google Drive API - Android - 如何获取驱动器文件 ID?

[英]Google Drive API - Android - How to obtain a drive file id?

I'm trying to develop an android app that can read a xml file stored in my google drive folder, the idea at first is trying to open the file and handle the content.我正在尝试开发一个可以读取存储在我的谷歌驱动器文件夹中的 xml 文件的 android 应用程序,最初的想法是尝试打开文件并处理内容。

I've read the Google Drive API docs for android and i reached a point that I'm lost, it's working with file contents.我已经阅读了适用于 androidGoogle Drive API 文档,但我已经迷失了,它正在处理文件内容。

According to this guide the way to open a file from drive is this:根据本指南,从驱动器打开文件的方法是:

DriveFile file = ...
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);`

Searching I realized that the complete code (that they not include there is):搜索我意识到完整的代码(它们不包括在内):

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,DriveId.bg(id));
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);`

Well the problem there is that I don't know the file "id".那么问题是我不知道文件“id”。 I've tried the id from the web link of google drive, something like this ( https://drive.google.com/open ? id=1EafJ-T6H4xI9VaUuUO5FMVb9Y30xyr7OHuISQ53avso &authuser=0) but didn´t work.我已经尝试过谷歌驱动器的网络链接中的 id,类似这样( https://drive.google.com/open ? id=1EafJ-T6H4xI9VaUuUO5FMVb9Y30xyr7OHuISQ53avso &authuser=0),但没有用。

You could use the DriveAPI Query method, to retrieve any information about an specific file.您可以使用 DriveAPI Query 方法来检索有关特定文件的任何信息。 you will need to define a query object as the following:您需要定义一个查询对象,如下所示:

Query query = new Query.Builder()
    .addFilter(Filters.eq(SearchableField.TITLE, "HelloWorld.java"))
    .build();

And set a callback function to iterate on the results:并设置一个回调函数来迭代结果:

Drive.DriveApi.query(googleApiClient, query)
        .setResultCallback(new OnChildrenRetrievedCallback() {

    @Override
    public void onChildrenRetrieved(MetadataBufferResult result) {
        // Iterate over the matching Metadata instances in mdResultSet
    }
});

You can find more information on the topic here: https://developers.google.com/drive/android/queries您可以在此处找到有关该主题的更多信息: https : //developers.google.com/drive/android/queries

The solution i found for this problem was creating the file from the app.我为这个问题找到的解决方案是从应用程序创建文件。 Using the class ( "CreateFileActivity.java" ) from google drive api demo app.使用 google drive api 演示应用程序中的类( “CreateFileActivity.java” )。

With this class i save the returning Driveid from the new file in a global DriveId variable.通过这个类,我将新文件中返回的 Driveid 保存在全局 DriveId 变量中。

final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
        ResultCallback<DriveFolder.DriveFileResult>() {
            @Override
            public void onResult(DriveFolder.DriveFileResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("","Error while trying to create the file");
                    return;
                }
                Id=result.getDriveFile().getDriveId();
                Log.e("","Created a file with content: " + Id);

            }
        };

Then with this id in another method i call the file and read it (If i want i can edit this file information from Google Drive Web App):然后在另一种方法中使用此 id 调用该文件并读取它(如果我愿意,我可以从 Google Drive Web App 编辑此文件信息):

 public void leer(){
       DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),Id);
       file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
               .setResultCallback(contentsOpenedCallback);
   }
ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("Error:","No se puede abrir el archivo o no se encuentra");
                    return;
                }
                // DriveContents object contains pointers
                // to the actual byte stream
                DriveContents contents = result.getDriveContents();
                BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String contentsAsString = builder.toString();
                Log.e("RESULT:",contentsAsString);
            }
        };

I've been playing with this stuff a few months back, and still have some code on github.几个月前我一直在玩这个东西,并且仍然在 github 上有一些代码。 It may be VERY outdated (libver 15 or so), but it may serve as a reference point, and it is (I hope simple).它可能非常过时(libver 15 左右),但它可以作为参考点,而且确实如此(我希望很简单)。 Look here .看这里 Pull it down, plug in, step through.将其拉下,插入,通过。 Fix what's not working anymore :-).修复不再起作用的东西:-)。 I've abandoned it some time ago.我前段时间放弃了。

Be aware of the fact that there are 2 different IDs for Google Drive Android API objects, see SO 22841237 .请注意,Google Drive Android API 对象有 2 个不同的 ID,请参阅SO 22841237

In general, you usually start with knowing the file/folder name, query GDAA to get a list of objects.通常,您通常从知道文件/文件夹名称开始,查询 GDAA 以获取对象列表。 Each of them will yield DriveID and ResourceID.它们中的每一个都会产生 DriveID 和 ResourceID。 DriveID is used in your app to manipulate the objects (does not mean anything outside your Android App and/or device). DriveID 在您的应用程序中用于操作对象(并不意味着您的 Android 应用程序和/或设备之外的任何内容)。 ResourceID is the string that appears in different forms in URLs and can be used outside your app (web browser for instance...). ResourceID 是在 URL 中以不同形式出现的字符串,可以在您的应用程序之外使用(例如 Web 浏览器...)。 Look at this wrapper to get some feeling how it works.看看这个包装器来感受一下它是如何工作的。 But again, it's been a few versions back, so there are no guaranties.但同样,它已经有几个版本了,所以没有保证。 Good Luck...祝你好运...

The Google Drive API is deprecated, now its Google Drive V3 and for Query we use不推荐使用 Google Drive API,现在它的 Google Drive V3 和我们使用的查询

String pageToken = null;
  do {
  FileList result = driveService.files().list()
  .setQ("mimeType='image/jpeg'")
  .setSpaces("drive")
  .setFields("nextPageToken, files(id, name)")
  .setPageToken(pageToken)
  .execute();
  for (File file : result.getFiles()) {
  System.out.printf("Found file: %s (%s)\n",
    file.getName(), file.getId());
  }
  pageToken = result.getNextPageToken();
  } 

  while (pageToken != null);

You can Learn more here Officals Docs您可以在此处了解更多信息官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Google Drive Android API删除Google Drive上的文件 - How to delete a file on google drive using Google Drive Android API 如何使用Google Drive Android API获取Drive_FILE_ID - how to get Drive_FILE_ID using Google Drive android API Google Drive Android API如何将音频文件上传到我的驱动器? 如何同步驱动器文件? - Google Drive Android API how to upload a audio file to my drive ? How to sync drive files? Java Google Drive Android API,上传后获取文件ID - Java Google Drive Android API, get file ID after upload 如何获取文件ID,以便我可以在Android上从Google Drive API下载文件? - How to get the file ID so I can perform a download of a file from Google Drive API on Android? 获取Google Drive API Android的资源ID - Get Resource Id For Google Drive API Android Google云端硬盘API:提供的文件ID不可用 - Google Drive API: The provided file ID is not usable Google Drive Android API-从驱动器下载数据库文件 - Google Drive Android api - Downloading db file from drive 如何使用 Google Drive API 从 android 将加密的图像文件上传到 Google Drive - How to upload encrypted image file on google drive from android using Google Drive API 如何使用Google Drive Android API更改在Google云端硬盘中创建的文件夹或文件的权限? - How to change permission of folder or file created in Google Drive using Google Drive Android API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM