创建或打开使用GoogleDriveApi自带谷歌文档 [英] Create or Open native Google documents using GoogleDriveApi

查看:167
本文介绍了创建或打开使用GoogleDriveApi自带谷歌文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在密切关注的谷歌云端硬盘Android API ,然后,所有的文档伟大工程。我可以创建新的文本文档,并使用文本的MIME类型读回/纯

我不能做的是创建本机谷歌文档或S preadsheet。其实,我可以通过使用MIME类型应用程序/ vnd.google-apps.document 应用程序/ vnd.google-apps.s创建它们preadsheet 按照支持的MIME类型文档

但是,如果我尝试写的内容这些文件,这些文件永远不会上传。
如果我尝试读取包含内容(内容我通过网络浏览器创建)我的 openContents 呼叫失败的文档。

同样,我可以创建文本/纯文件,并写信给他们,但他们都没有自带谷歌文档。我已经scowered的文档和示例文件,但没有描述了我要找的。

这看起来很基本的。请问新GoogleApiClient不支持这样做呢?我在想什么或做错了吗?

下面是核心code创建。当试图读取应用程序/ vnd.google-apps.document ,但我敢肯定,这两个问题都与我有类似的问题。我就饶读code的详细程度。

 私人无效exportToGDriveFile(){
    Drive.DriveApi.newContents(getGoogleApiClient())setResultCallback(createNewFileCallback)。
}最后的私人ResultCallback< ContentsResult> createNewFileCallback =新ResultCallback< ContentsResult>(){    @覆盖
    公共无效onResult(ContentsResult结果){        如果(!result.getStatus()。isSuccess()){
            WRITELOG(试图创建新文件内容错误);
            返回;
        }        字符串文件名= getIncrementedFileName();
        MetadataChangeSet变更=新MetadataChangeSet.Builder()
                .setTitle(文件名)
                .setMimeType(text / plain的)//< - 这工作!我可以写和读回:)
                //.setMimeType(\"application/vnd.google-apps.document)< - 可创建如果不包括的内容。
                //.setMimeType(\"application/vnd.google-apps.s$p$padsheet)
                .setStarred(真)
                。建立();        WRITELOG(创建文件:+文件名);        //创建根文件夹的文件
        Drive.DriveApi.getRootFolder(getGoogleApiClient())
                .createFile(getGoogleApiClient(),变更,result.getContents())
                .setResultCallback(afterCreateFileCallback);
    }
};私人ResultCallback< D​​riveFileResult> afterCreateFileCallback =新ResultCallback< D​​riveFileResult>(){    @覆盖
    公共无效onResult(DriveFileResult结果){        如果(!result.getStatus()。isSuccess()){
            WRITELOG(错误尝试创建文件);
            返回;
        }        DriveFile driveFile = result.getDriveFile();
        WRITELOG(创建文件+ driveFile.getDriveId());
        新WriteFileAsyncTask()执行(driveFile);    }
};私有类WriteFileAsyncTask扩展的AsyncTask< D​​riveFile,太虚,布尔> {    @覆盖
    保护布尔doInBackground(DriveFile参数... args){        DriveFile文件= ARGS [0];
        尝试{
            ContentsResult contentsResult = file.openContents(getGoogleApiClient(),DriveFile.MODE_WRITE_ONLY,NULL).await();
            如果(!contentsResult.getStatus()。isSuccess()){
                返回false;
            }
/ ************************
如果我尝试到了这里,写的内容'应用程序/ vnd.google-apps.document`文件将无法上传。
******** /            字符串内容=Hello World的;            为OutputStream的OutputStream = contentsResult.getContents()的getOutputStream()。
            outputStream.write(contents.getBytes());
            com.google.android.gms.common.api.Status状态= file.commitAndCloseContents(
                    。getGoogleApiClient(),contentsResult.getContents())等待();
            返回status.getStatus()isSuccess()。
        }赶上(IOException异常五){
            //吐司(IOException异常,而追加到输出流);
        }        返回false;
    }    @覆盖
    保护无效onPostExecute(布尔结果){        如果(!结果){
            //吐司(错误在编辑内容);
            返回;
        }
        //吐司(成功上传Quizifications!);
    }}


解决方案

这是目前无法读取或编辑文档谷歌,S preadsheets或presentation文件的内容。这些文件是没有标准的二进制内容的一种特殊类型的,所以你不能阅读和他们从其他文件可以在同样的方式写。

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

很抱歉的混乱,我们应该更新的行为,使其明确表示其不可能的。

I have been closely following the documentation for the Google Drive Android API and, all works great. I can create new text documents and read them back in using the mime type of text/plain.

What I cannot do is create a native Google "Document" or "Spreadsheet." Actually, I can create them by using the mime type to application/vnd.google-apps.document or application/vnd.google-apps.spreadsheet as per Supported MIME Types documentation.

If, however, I try to write content to these documents, the documents never get uploaded. If I try to read documents that have content (content I created via a web browser) my openContents call fails.

Again, I can create text/plain documents and write to them, but they are not native Google Documents. I have scowered the documentation and sample files, but nothing describes what I'm looking for.

This seems so basic. Does the new GoogleApiClient not support doing this? What am I missing or doing wrong?

Here is the core code for creating. I have a similar issue when trying to read a application/vnd.google-apps.document but I'm sure the two issues are related. I'll spare the verbosity of "read" code.

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!");
    }

}

解决方案

It's not currently possible to read or edit the contents of Google Documents, Spreadsheets or Presentation files. They files are of a special type that don't have standard binary content, so you can't read and write from them in the same way you can from other files.

You can, however, interact with the metadata of existing files.

Sorry for the confusion, we should update the behavior so that its clear that its not possible.

这篇关于创建或打开使用GoogleDriveApi自带谷歌文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆