[英]Google Drive import Google Docs
我有以下代码用于获取 Google Drive 中文件的DriveContents
。 我能够导入和获取 MS Word、文本文件等的DriveContents
,但是当文件是 Google 原生的(Google Doc、Google Sheets 等)时,我无法获取内容。 我的代码如下:
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 格式的文件时,它只会返回一个不成功的结果并在我的日志中显示错误。 我怎样才能获得这些文件的文件内容? 有什么特别我应该做的吗?
我正在阅读以下文档:
不确定这是否是最好的解决方案,但我是按照以下方式完成的。 我检查元数据是否是 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);
完成上述操作后,您可以使用 API 获取文件:
https://developers.google.com/drive/web/manage-downloads
所述token
从上面给出的Authentication
报头上的下载令牌。 我们可以将文件导出为 docx、pdf 等,然后以这种方式下载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.