谷歌驱动器API - Android -如何获得驱动器文件id?

时间:2021-04-09 15:23:23

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.

我正在开发一个android应用程序,它可以读取存储在谷歌驱动器文件夹中的xml文件,最初的想法是尝试打开文件并处理内容。

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.

我已经阅读了android的谷歌驱动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". 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。我试过谷歌的网页链接的id,这样(https://drive.google.com/open?id=1EafJ-T6H4xI9VaUuUO5FMVb9Y30xyr7OHuISQ53avso&authuser=0)但´t工作。

3 个解决方案

#1


7  

You could use the DriveAPI Query method, to retrieve any information about an specific file. you will need to define a query object as the following:

您可以使用DriveAPI查询方法来检索关于特定文件的任何信息。您需要定义一个查询对象如下所示:

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

#2


3  

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.

我发现解决这个问题的方法是从应用程序创建文件。使用谷歌驱动器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并读取它(如果我想的话,我可以从谷歌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);
            }
        };

#3


1  

I've been playing with this stuff a few months back, and still have some code on github. It may be VERY outdated (libver 15 or so), but it may serve as a reference point, and it is (I hope simple). Look here. Pull it down, plug in, step through. Fix what's not working anymore :-). I've abandoned it some time ago.

几个月前,我一直在玩这些东西,但在github上仍然有一些代码。它可能非常过时(libver15左右),但它可以作为一个参考点,而且它是(我希望很简单)。看这里。拉下来,插上电源,走过去。修复什么不再工作:)。我早就放弃了。

Be aware of the fact that there are 2 different IDs for Google Drive Android API objects, see SO 22841237.

请注意谷歌驱动Android API对象有两个不同的id,请参见SO 22841237。

In general, you usually start with knowing the file/folder name, query GDAA to get a list of objects. Each of them will yield DriveID and ResourceID. DriveID is used in your app to manipulate the objects (does not mean anything outside your Android App and/or device). ResourceID is the string that appears in different forms in URLs and can be used outside your app (web browser for instance...). 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...

通常,您首先要知道文件/文件夹名称,查询GDAA以获取对象列表。它们中的每一个都将产生DriveID和ResourceID。你的应用程序中使用的是DriveID来操纵这些对象(并不意味着你的Android应用程序和/或设备之外的任何东西)。ResourceID是在url中以不同形式出现的字符串,可以在应用程序之外使用(例如web浏览器…)。看看这个包装来了解它是如何工作的。但同样的,之前也有一些版本,所以没有担保。祝你好运…

#1


7  

You could use the DriveAPI Query method, to retrieve any information about an specific file. you will need to define a query object as the following:

您可以使用DriveAPI查询方法来检索关于特定文件的任何信息。您需要定义一个查询对象如下所示:

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

#2


3  

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.

我发现解决这个问题的方法是从应用程序创建文件。使用谷歌驱动器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并读取它(如果我想的话,我可以从谷歌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);
            }
        };

#3


1  

I've been playing with this stuff a few months back, and still have some code on github. It may be VERY outdated (libver 15 or so), but it may serve as a reference point, and it is (I hope simple). Look here. Pull it down, plug in, step through. Fix what's not working anymore :-). I've abandoned it some time ago.

几个月前,我一直在玩这些东西,但在github上仍然有一些代码。它可能非常过时(libver15左右),但它可以作为一个参考点,而且它是(我希望很简单)。看这里。拉下来,插上电源,走过去。修复什么不再工作:)。我早就放弃了。

Be aware of the fact that there are 2 different IDs for Google Drive Android API objects, see SO 22841237.

请注意谷歌驱动Android API对象有两个不同的id,请参见SO 22841237。

In general, you usually start with knowing the file/folder name, query GDAA to get a list of objects. Each of them will yield DriveID and ResourceID. DriveID is used in your app to manipulate the objects (does not mean anything outside your Android App and/or device). ResourceID is the string that appears in different forms in URLs and can be used outside your app (web browser for instance...). 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...

通常,您首先要知道文件/文件夹名称,查询GDAA以获取对象列表。它们中的每一个都将产生DriveID和ResourceID。你的应用程序中使用的是DriveID来操纵这些对象(并不意味着你的Android应用程序和/或设备之外的任何东西)。ResourceID是在url中以不同形式出现的字符串,可以在应用程序之外使用(例如web浏览器…)。看看这个包装来了解它是如何工作的。但同样的,之前也有一些版本,所以没有担保。祝你好运…