【发布时间】:2016-08-09 03:39:01
【问题描述】:
我目前正在开发一个应用程序,用户可以在该应用程序中从他们的 Google 云端硬盘中选择一个 Google 表格文件 (.xlxs)。然后,我的应用程序将提取并处理该工作表的某些内容。
我正在使用为 Android 开发的 Google Drive API,并使用来自 Google Drive Android Demos 的示例类
到目前为止,我已经实现了一个显示用户云端硬盘文件的云端硬盘文件选择器。选择一个文件会返回该文件的 ID。
第一个问题是我似乎无法设置 mime 类型,因此只能选择 xlsl 文件。谷歌搜索告诉我
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
是正确的 mime 类型。
我在这种情况下使用它
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
.setMimeType(new String[]{"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
.build(getGoogleApiClient());
我以前没有遇到过 mime 类型,但是“text/plain”作为 meme 类型似乎很有效,并且它们可以在相同的 mime 类型列表中找到..
第二个问题基本上是关于我应该如何从工作表中的某些列和行中提取字符串。
这是处理所选文件内容的代码(当然可以修改,就是不知道怎么修改)。
protected String doInBackgroundConnected(DriveId... params) {
String contents = null;
DriveFile file = params[0].asDriveFile();
DriveContentsResult driveContentsResult =
file.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).await();
if (!driveContentsResult.getStatus().isSuccess()) {
return null;
}
DriveContents driveContents = driveContentsResult.getDriveContents();
BufferedReader reader = new BufferedReader(
new InputStreamReader(driveContents.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
builder.append(line);
}
contents = builder.toString();
} catch (IOException e) {
Log.e(TAG, "IOException while reading from the stream", e);
}
driveContents.discard(getGoogleApiClient());
return contents;
}
【问题讨论】:
标签: java android google-drive-api