我有一个 Android 应用程序,允许用户将 .csv 文件导出到 Google 云端硬盘,以便他们可以对其进行编辑,然后将文件重新导入到 Android 中。
这曾经与旧的 Google Docs api 完美配合。去年我从那个旧的 API 升级到 Google Drive 的早期版本并且能够让它运行得很好。现在,当我升级到最新版本时,我的应用程序的这个功能实际上被破坏了。我认为这是 SDK 权限和从 Google 云端硬盘中分离出 Google 表格的组合。
当我上传一个包含此元数据的文件时会发生什么:
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle(fileName)
.setMimeType("text/csv")
.setStarred(true)
.build();
上传的文件会带有一个蓝色的 Google 文档图标。当用户访问 Google Drive 进行编辑时,他们只能“预览”文档。当他们预览它时,他们可以使用 Google 表格“打开”文档,然后创建一个新文档(这个新文档有绿色的 Google 表格图标)并且由于 Google SDK 有这个新的“功能”(引自:https://developers.google.com/drive/android/queries) :
Note: The Android Drive API only works with the https://www.googleapis.com/auth/drive.file scope. This means that only files which a user has opened or created with your application can be matched by a query.
我无法看到新文件,因此无法检索用户的编辑。
有什么办法可以直接将其作为 Google 表格文件上传吗?或者我可能完全错过了另一种解决方案?
编辑
这是一个关于我如何使用 Google Drive SDK 上传我的代码的示例:
在我的 AsyncTask 的构造函数中,我创建了谷歌客户端:
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
然后在 doInBackground 方法中我做这样的事情:
DriveApi.DriveContentsResult cResult = Drive.DriveApi.newDriveContents(mGoogleApiClient).await();
OutputStream os = cResult.getDriveContents().getOutputStream();
InputStream is = new FileInputStream(f);
//write the input stream to the output stream
....
DriveFileResult exportResult = Drive.DriveApi.getRootFolder(mGoogleApiClient)
.createFile(mGoogleApiClient, changeSet, cResult.getDriveContents())
.await();
这很简单(一旦你弄清楚了)似乎没有办法告诉 SDK 我希望这个文档是“Google 表格”而不是“Google 文档”
谢谢!
-亚伦
最佳答案
您应该将原始文件明确转换为电子表格,否则最终会直接上传到您的云端硬盘帐户。试试下面的代码:
java.io.File fileContent = new java.io.File(csvfilepath);
FileContent mediaContent = new FileContent("text/csv", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/csv");
Insert request = service.files().insert(body, mediaContent);
request.setConvert(true);
File file = request.execute();
关于android - 使用 Android SDK 将 CSV 文件上传为 Google 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27733919/