下面的代码用于获取Google中文件的DriveContents。我能够导入并获取import、文本文件等的DriveContents,但是当该文件是谷歌的原生文件时(Google、Google等)我拿不到里面的东西。我的代码如下:
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格式文件时,它只返回一个不成功的结果,并在我的日志中显示错误。如何获得这些文件的文件内容?我有什么特别的事要做吗?
我正在阅读以下文档:
发布于 2015-10-07 16:06:07
不确定这是否是最好的解决方案,但我是这样做的。如果元数据是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等,并下载它的方式。
https://stackoverflow.com/questions/32975533
复制相似问题