我想以编程方式在Google云端硬盘上创建电子表格

时间:2016-11-15 06:38:23

标签: android android-studio google-drive-api google-drive-android-api

以下代码用于创建电子表格。

MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                    .setTitle("New file")
                    //.setMimeType("text/plain")
                    .setMimeType("application/vnd.google-apps.spreadsheet")
                    .setStarred(true).build();

            // create a file on root folder
            Drive.DriveApi.getRootFolder(getGoogleApiClient())
                    .createFile(getGoogleApiClient(), changeSet, driveContents)
                    .setResultCallback(fileCallback);

如果我使用Mime类型application/vnd.google-apps.spreadsheet,那么它会显示错误而不会创建任何电子表格。如果我使用Mime类型text/plain,那么它会创建一个文件。所以请帮助我......

当我尝试创建Google电子表格时出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: Thread-2475
      Process: com.google.android.gms.drive.sample.demo, PID: 31762
      java.lang.IllegalArgumentException: May not create shortcut files using this method. Use DriveFolder.createShortcutFile() instead.
                  at com.google.android.gms.drive.internal.zzy.zzb(Unknown Source)
                  at com.google.android.gms.drive.internal.zzy.createFile(Unknown Source)
                  at com.google.android.gms.drive.sample.demo.CreateFileActivity$1$1.run(CreateFileActivity.java:82)

1 个答案:

答案 0 :(得分:0)

不幸的是你不能这样做。

创建文件方法是这样的:

Drive.DriveApi.getRootFolder(getGoogleApiClient())
                    .createFile

if (mimeType.equals("application/vnd.google-apps.folder") || mimeType.startsWith("application/vnd.google-apps")) {
  throw new IllegalArgumentException("May not create shortcut files using this method. Use DriveFolder.createShortcutFile() instead.);
}

为此,您必须使用REST API https://developers.google.com/drive/v2/reference/