私はGoogle Drive Android APIのドキュメントを注意深くフォローしてきましたが、すべてうまく機能しています。の MIME タイプを使用して、新しいテキスト ドキュメントを作成し、読み返すことができますtext/plain
。
私ができないことは、ネイティブの Google「ドキュメント」または「スプレッドシート」を作成することです。実際には、MIME タイプを使用して、application/vnd.google-apps.document
またはサポートされている MIME タイプのapplication/vnd.google-apps.spreadsheet
ドキュメントに従って作成できます。
ただし、これらのドキュメントにコンテンツを書き込もうとすると、ドキュメントはアップロードされません。コンテンツ (Web ブラウザで作成したコンテンツ) を含むドキュメントを読み込もうとすると、openContents
呼び出しに失敗します。
繰り返しますが、text/plain
ドキュメントを作成して書き込むことはできますが、ネイティブの Google ドキュメントではありません。ドキュメンテーションとサンプル ファイルを調べましたが、私が探しているものについては何も説明されていません。
これはとても基本的なようです。新しい GoogleApiClient はこれをサポートしていませんか? 私は何が欠けているか、間違っていますか?
作成するためのコアコードは次のとおりです。を読み取ろうとすると同様の問題が発生しapplication/vnd.google-apps.document
ますが、2 つの問題が関連していると確信しています。「読み取り」コードの冗長性は割愛します。
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!");
}
}