简体   繁体   English

Google 云端硬盘导入 Google 文档

[英]Google Drive import Google Docs

I have the following code for getting DriveContents of a file in Google Drive.我有以下代码用于获取 Google Drive 中文件的DriveContents I'm able to import and get DriveContents of MS Word, text files, etc but when the file is native to Google (Google Doc, Google Sheets, etc.) i'm not able to get the contents.我能够导入和获取 MS Word、文本文件等的DriveContents ,但是当文件是 Google 原生的(Google Doc、Google Sheets 等)时,我无法获取内容。 My code is below:我的代码如下:

selectedFile.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
    public void onResult(DriveApi.DriveContentsResult result) {
        try {
            if (!result.getStatus().isSuccess()) {
                // display an error saying file can't be opened
                Log.e(TAG, "Could not get file contents");
                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 (Exception e) {
                e.printStackTrace();
            }
            String contentsAsString = builder.toString();

            contents.discard(getGoogleApiClient());

            Log.i(TAG, contentsAsString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

Whenever I get a Google format file, it simply returns a result that is not a success and shows the error in my logs.每当我获得 Google 格式的文件时,它只会返回一个不成功的结果并在我的日志中显示错误。 How can I get the file contents of those files as well?我怎样才能获得这些文件的文件内容? Is there something special i'm supposed to do?有什么特别我应该做的吗?

I'm reading the following documentation:我正在阅读以下文档:

https://developers.google.com/drive/android/files https://developers.google.com/drive/android/files

Not sure if this is the best solution but I did it the following way.不确定这是否是最好的解决方案,但我是按照以下方式完成的。 I check in the metadata if it's a Google format (doc, sheets, etc) and if it is, I have an AsyncTask that does the below:我检查元数据是否是 Google 格式(文档、表格等),如果是,我有一个执行以下操作的AsyncTask

// accountName is the email address the user used when choosing which account
String scope = "oauth2:https://www.googleapis.com/auth/drive.file";
token = GoogleAuthUtil.getTokenWithNotification(fragment.getActivity(), accountName, scope, null);

After doing the above, you can get the file using the API:完成上述操作后,您可以使用 API 获取文件:

https://developers.google.com/drive/web/manage-downloads https://developers.google.com/drive/web/manage-downloads

The token from the above gives the Authentication header token on the downloading.所述token从上面给出的Authentication报头上的下载令牌。 We can export the file as docx, pdf, etc and download it that way.我们可以将文件导出为 docx、pdf 等,然后以这种方式下载。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM