Google 驱动器导入 Google 文档
Google Drive import Google Docs
我有以下代码用于获取 Google 驱动器中文件的 DriveContents
。我能够导入并获取 DriveContents
的 MS Word、文本文件等,但是当文件是 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 格式的文件时,它只是 returns 一个不成功的结果,并在我的日志中显示错误。我怎样才能得到这些文件的文件内容呢?我有什么特别的事情要做吗?
我正在阅读以下文档:
不确定这是否是最佳解决方案,但我是按照以下方式完成的。我检查元数据是否为 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
header 令牌。我们可以将文件导出为 docx、pdf 等格式并以这种方式下载。
我有以下代码用于获取 Google 驱动器中文件的 DriveContents
。我能够导入并获取 DriveContents
的 MS Word、文本文件等,但是当文件是 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 格式的文件时,它只是 returns 一个不成功的结果,并在我的日志中显示错误。我怎样才能得到这些文件的文件内容呢?我有什么特别的事情要做吗?
我正在阅读以下文档:
不确定这是否是最佳解决方案,但我是按照以下方式完成的。我检查元数据是否为 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
header 令牌。我们可以将文件导出为 docx、pdf 等格式并以这种方式下载。