I pick Google Spreadsheet with Intent.ACTION_OPEN_DOCUMENT and can get it's Virtual File URI. But to use Google Sheets API, I need Spreadsheet ID, and it seems Virtual File URI doesn't contain it. How can I get Google Spreadsheet ID from data, received by Intent.ACTION_OPEN_DOCUMENT?
My simplified code snippet:
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
}
}
Further researches show that Intent.ACTION_VIEW successfully find required spreadsheet by virtual file URI:
if (isVirtualFile(uri)) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} }
and actual activity class is com.google.android.apps.docs.app.OpenSafUrlActivity
.
It would be nice to look into the code, but I could not find sources...
UPDATE
It seems that i've found a workaround here: Find a Google Drive Sheet By Name Using API 4 In Android