【问题标题】:Create or Open native Google documents using GoogleDriveApi使用 GoogleDriveApi 创建或打开原生 Google 文档
【发布时间】:2014-04-26 10:00:32
【问题描述】:

我一直在密切关注Google Drive Android API 的文档,并且一切正常。我可以使用 text/plain 的 mime 类型创建新的文本文档并将它们读回。

我不能做的是创建原生 Google“文档”或“电子表格”。实际上,我可以按照Supported MIME Types 文档使用application/vnd.google-apps.documentapplication/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


    【解决方案1】:

    目前无法读取或编辑 Google 文档、电子表格或演示文稿文件的内容。它们是一种特殊类型的文件,没有标准的二进制内容,因此您无法像从其他文件中那样读取和写入它们。

    但是,您可以与现有文件的元数据进行交互。

    抱歉造成混淆,我们应该更新行为,以明确其不可能。

    【讨论】:

    【解决方案2】:

    使用 HTML 更新 Google 文档很简单。只需在正文中使用 html 格式的文本(需要 html 标记)和 content-type 为 google docs 发出 api 请求,然后您创建/更新的文件将作为具有所有格式选项的 Google Doc 提供给用户。

               request({
                 uri: 'https://www.googleapis.com/upload/drive/v2/files/'+fileId,
                 method: 'PUT',
                 qs: {
                   uploadType: 'media'
                 },
                 form: '<html> Hello <b>World!</b> </html>',
                 headers: {
                   'Content-Type': 'application/vnd.google-apps.document',
                   'Authorization': 'Bearer ' + access_token
                 }},  function (error, response, body){
    
    
               })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-29
      • 2016-07-22
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多