[英]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.