如何解决从Google Storage Access Framework虚拟文件URI获取Google Spreadsheet ID
我选择具有Intent.ACTION_OPEN_DOCUMENT的Google Spreadsheet,可以获取它的虚拟文件URI。 但是要使用Google Sheets API,我需要电子表格ID,而且似乎虚拟文件URI不包含它。 如何从Intent.ACTION_OPEN_DOCUMENT收到的数据中获取Google Spreadsheet ID?
我的简化代码段:
public class MainActivity extends AppCompatActivity {
...
public void onMenuFile(MenuItem item) {
// called when 'File' menu item is selected
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
startActivityForResult(intent,EDIT_REQUEST_CODE);
}
...
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode != EDIT_REQUEST_CODE || resultCode != Activity.RESULT_OK || data == null) {
return;
}
uri = data.getData();
if (isVirtualFile(uri)) {
// here i would like to get spreadsheetID somehow,and use it:
ValueRange response = service.spreadsheets().values()
.get(spreadsheetID,sheetTitle)
.execute();
List<List<Object>> values = response.getValues();
...and so on...
}
...
}
...
private boolean isVirtualFile(URI uri)) {
here code from
https://developer.android.com/training/data-storage/shared/documents-files#java
}
}
进一步的研究表明,Intent.ACTION_VIEW通过虚拟文件URI成功找到了所需的电子表格:
if (isVirtualFile(uri)) {
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} }
实际活动类别为com.google.android.apps.docs.app.OpenSafUrlActivity
。
最好看一下代码,但我找不到源...
更新
似乎我在这里找到了解决方法: Find a Google Drive Sheet By Name Using API 4 In Android
解决方法
SpreadsheetID需要进行硬编码。请参见以下Google代码段:
您只能在创建电子表格对象之后调用getData()。
要找出电子表格ID是什么,请转到Google驱动器上所需的表并查看URL:
https://docs.google.com/spreadsheets/d/**spreadsheetId**/edit#gid=0
此处有更多信息:https://developers.google.com/sheets/api/guides/concepts
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。