Google Drive API - Android - 如何获取驱动器文件ID? [英] Google Drive API - Android - How to obtain a drive file id?

查看:185
本文介绍了Google Drive API - Android - 如何获取驱动器文件ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已阅读 Android版Google Drive API文档,并且我达到了一个要点我失去了,它正在处理文件内容。



根据本指南,从drive打开文件的方式是这样的:

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

搜索我意识到完整的代码(它们不包含):

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



<那么问题在于我不知道文件id。我已经尝试了谷歌驱动器的网页链接的ID,如此( https://drive.google.com/open id = 1EafJ-T6H4xI9VaUuUO5FMVb9Y30xyr7OHuISQ53avso & authuser = 0)但没有成功。

我发现这个问题的解决方案是从应用程序创建文件。使用该类( CreateFileActivity.java )来自Google Drive api演示程序。



来自全局DriveId变量中的新文件。

 最终专用ResultCallback< DriveFolder.DriveFileResult> fileCallback = new 
ResultCallback< DriveFolder.DriveFileResult>(){
@Override $ b $ public void onResult(DriveFolder.DriveFileResult result){
if(!result.getStatus()。isSuccess ()){
Log.e(,尝试创建文件时出错);
return;
}
Id = result.getDriveFile()。getDriveId();
Log.e(,创建一个包含内容的文件:+ Id);

}
};

然后在另一个方法中使用这个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 en encuentra);
return;
}
// DriveContents对象包含指针
//指向实际的字节流
DriveContents contents = result.getDriveContents();
BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
StringBuilder builder = new StringBuilder();
字符串行; ((line = reader.readLine())!= null){
builder.append(line);
尝试{
while
}
} catch(IOException e){
e.printStackTrace();
}
String contentsAsString = builder.toString();
Log.e(RESULT:,contentsAsString);
}
};


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.

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.

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.

解决方案

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.

With this class i save the returning Driveid from the new file in a global DriveId variable.

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):

 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);
            }
        };

这篇关于Google Drive API - Android - 如何获取驱动器文件ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆