【问题标题】:Updating new revision for google drive native documents更新谷歌驱动原生文档的新版本
【发布时间】:2013-09-26 19:36:13
【问题描述】:

我在谷歌驱动器中创建了一个文档。我想使用 google drive android sdk 为同一文档上传新修订版。我试过这样的代码,

try{
                                                                            // First retrieve the file from the API.
  File file = service.files().get(fileId).execute();

java.io.File fileContent = new java.io.File("sdcard0/temp/test.doc");
FileContent mediaContent = new FileContent("application/vnd.google-apps.document", fileContent);

File updatedFile = service.files().update(getID(), file, mediaContent).execute();                                   
} catch (IOException e1) {
       Log.d("","An error occurred: " + e1); //No i18n
} catch (Exception e){
       Log.d("","EXCEPTION IN SAVING"+e); //No i18n
}

但是 docs.google.com 中的内容看起来像 一样已损坏

如果我做错了什么,请指导我。

注意:相同的代码适用于上传的文档。

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    您不能将这些修订版用于谷歌文档等原生格式。那些有自己的api来修改它们。例如电子表格有电子表格提要 api。

    【讨论】:

    • 我可以知道怎么做吗?
    【解决方案2】:

    您可以使用update request 中的转换参数为 Google 文档/电子表格格式创建新修订。

    按照您的代码修改以在上传时启用转换(未经测试但确信它是正确的)

    try{
        // First retrieve the file from the API.
        File file = service.files().get(fileId).execute();
    
        java.io.File fileContent = new java.io.File("sdcard0/temp/test.doc");
        FileContent mediaContent = new FileContent("application/msword", fileContent); //Changed the mime type to original
    
        File updatedFile = service.files().update(getID(), file, mediaContent)
                            .setConvert(true) //Convert the file while uploading
                            .execute();                                   
    } catch (IOException e1) {
           Log.d("","An error occurred: " + e1); //No i18n
    } catch (Exception e){
           Log.d("","EXCEPTION IN SAVING"+e); //No i18n
    }
    

    【讨论】:

    • 我认为是android代码,但你能确认一下吗?我会尝试编写一个测试应用程序。我在我的 Android 应用程序中使用了类似的代码,但以 txt 文件作为源,并且文件被正确转换(使用插入,可能在转换更新时存在错误)
    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    相关资源
    最近更新 更多