【问题标题】:Upload a new document to Google drive from my android application从我的 android 应用程序将新文档上传到 Google 驱动器
【发布时间】:2012-10-09 20:42:13
【问题描述】:

我正在尝试使用 Google Drive SDK 从我的 android 应用程序在 Google Drive 上创建一个新文档。我的身份验证正在运行,并且我已经设法使用以下代码创建了一个简单的文本文件:

// com.google.api.services.drive.model.File
File newFile = new File();
newFile.setTitle("Test");
newFile.setMimeType("text/plain");
String contentStr = "This is a test file";      

try {
    File insertedFile = drive.files().insert(
         newFile, ByteArrayContent.fromString("text/plain", contentStr)).execute();
} catch (IOException e) {
    e.printStackTrace();
}

如果我将“text/plain”更改为“application/vnd.google-apps.document”并插入没有任何内容的文件,它可以正常工作,并在我的 Google Drive 上创建一个新文档:

// com.google.api.services.drive.model.File
File newFile = new File();
newFile.setTitle("Test");
newFile.setMimeType("application/vnd.google-apps.document");   

try {
    File insertedFile = drive.files().insert(newFile).execute();
} catch (IOException e) {
     e.printStackTrace();
}

但我想上传一个包含内容的新文档,但使用与纯文本文件相同的方法时:

// com.google.api.services.drive.model.File
File newFile = new File();
newFile.setTitle("Test");

String fileType = "application/vnd.google-apps.document";
newFile.setMimeType(fileType);   

try {
    File insertedFile = drive.files().insert(
              newFile, ByteArrayContent.fromString(fileType, contentStr)).execute();
} catch (IOException e) {
    e.printStackTrace();
}

我收到此错误:

 com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
 {
     "code" : 400,
     "errors" : [ {
          "domain" : "global",
          "message" : "Bad Request",
          "reason" : "badRequest"
     } ],
     "message" : "Bad Request"
 }

并且没有创建文件。请问有人可以帮我吗?

【问题讨论】:

    标签: android google-drive-api


    【解决方案1】:

    mime 类型必须是源文档的mime 类型,例如text/plan 或text/HTML。如果您使用 convert=true 上传,则云端硬盘上生成的文件将具有 application/vnd.google-apps.document 的 MIME 类型。

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2013-07-06
    相关资源
    最近更新 更多