【发布时间】:2014-04-26 10:00:32
【问题描述】:
我一直在密切关注Google Drive Android API 的文档,并且一切正常。我可以使用 text/plain 的 mime 类型创建新的文本文档并将它们读回。
我不能做的是创建原生 Google“文档”或“电子表格”。实际上,我可以按照Supported MIME Types 文档使用application/vnd.google-apps.document 或application/vnd.google-apps.spreadsheet 的mime 类型来创建它们。
但是,如果我尝试将内容写入这些文档,则这些文档永远不会被上传。
如果我尝试阅读包含内容(通过网络浏览器创建的内容)的文档,我的openContents 调用将失败。
同样,我可以创建 text/plain 文档并写入它们,但它们不是本地 Google 文档。我浏览了文档和示例文件,但没有描述我要查找的内容。
这似乎很基本。新的 GoogleApiClient 不支持这样做吗?我错过了什么或做错了什么?
这里是创建的核心代码。我在尝试阅读application/vnd.google-apps.document 时遇到了类似的问题,但我确信这两个问题是相关的。我将省去“阅读”代码的冗长。
private void exportToGDriveFile() {
Drive.DriveApi.newContents(getGoogleApiClient()).setResultCallback(createNewFileCallback);
}
final private ResultCallback<ContentsResult> createNewFileCallback = new ResultCallback<ContentsResult>() {
@Override
public void onResult(ContentsResult result) {
if (!result.getStatus().isSuccess()) {
writeLog("Error while trying to create new file contents");
return;
}
String fileName = getIncrementedFileName();
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(fileName)
.setMimeType("text/plain") // <-- This works! I can write and read back :)
//.setMimeType("application/vnd.google-apps.document") <-- can create if no contents are included.
//.setMimeType("application/vnd.google-apps.spreadsheet")
.setStarred(true)
.build();
writeLog("creating file: " + fileName);
// create a file on root folder
Drive.DriveApi.getRootFolder(getGoogleApiClient())
.createFile(getGoogleApiClient(), changeSet, result.getContents())
.setResultCallback(afterCreateFileCallback);
}
};
private ResultCallback<DriveFileResult> afterCreateFileCallback = new ResultCallback<DriveFileResult>() {
@Override
public void onResult(DriveFileResult result) {
if (!result.getStatus().isSuccess()) {
writeLog("Error while trying to create the file");
return;
}
DriveFile driveFile = result.getDriveFile();
writeLog("Created file " + driveFile.getDriveId());
new WriteFileAsyncTask().execute(driveFile);
}
};
private class WriteFileAsyncTask extends AsyncTask<DriveFile, Void, Boolean> {
@Override
protected Boolean doInBackground(DriveFile... args) {
DriveFile file = args[0];
try {
ContentsResult contentsResult = file.openContents(getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
if (!contentsResult.getStatus().isSuccess()) {
return false;
}
/************************
If I try to write content here, `application/vnd.google-apps.document` files will not upload.
*************************/
String contents = "Hello World";
OutputStream outputStream = contentsResult.getContents().getOutputStream();
outputStream.write(contents.getBytes());
com.google.android.gms.common.api.Status status = file.commitAndCloseContents(
getGoogleApiClient(), contentsResult.getContents()).await();
return status.getStatus().isSuccess();
} catch (IOException e) {
// toast("IOException while appending to the output stream");
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (!result) {
// toast("Error while editing contents");
return;
}
// toast("Successfully uploaded Quizifications!");
}
}
【问题讨论】:
标签: android google-drive-api document spreadsheet google-drive-android-api