首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google导入Google文档

Google导入Google文档
EN

Stack Overflow用户
提问于 2015-10-06 16:59:03
回答 1查看 132关注 0票数 2

下面的代码用于获取Google中文件的DriveContents。我能够导入并获取import、文本文件等的DriveContents,但是当该文件是谷歌的原生文件时(Google、Google等)我拿不到里面的东西。我的代码如下:

代码语言:javascript
复制
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();
        }
    }
});

每当我得到一个Google格式文件时,它只返回一个不成功的结果,并在我的日志中显示错误。如何获得这些文件的文件内容?我有什么特别的事要做吗?

我正在阅读以下文档:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-07 16:06:07

不确定这是否是最好的解决方案,但我是这样做的。如果元数据是Google格式(文档、工作表等),那么我将签入元数据,如果是的话,我有一个AsyncTask,它执行以下操作:

代码语言:javascript
复制
// 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);

完成上述操作后,您可以使用API获得文件:

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

上面的token在下载时给出了Authentication头令牌。我们可以导出文件作为docx,pdf等,并下载它的方式。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32975533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档