Is there any way to get Google Sheets fileid from file picker intent?

Viewed 142

In my Android app I would like the user to open a Google Sheets document to import data from it into the app database.

  1. It's easy to select a file with ACTION_GET_CONTENT / ACTION_OPEN_DOCUMENT and with MIME filter application/vnd.google-apps.spreadsheet. (It will be a virtual file.)

  2. It's also easy to get the content of a Google Sheets document with the Google Sheets API based on the Drive filed id (with user-granted spreadsheets.readonly scope):

Sheets.Builder(transport, jsonFactory, appCredential)
   .build()
   .spreadsheets()
   .get(fileid)
   .execute()

But the first step returns the Uri of the file on the device, and the second step requires the file id of the Drive file.
How can I determine the second from the first?

The only poor solution I found was to use the Google Drive API: I search the Sheets file with its name I got from the intent, and I get the id of it... It requires the drive.metadata.readonly scope, which is a restricted scope, and doh... it's not a viable solution anyway...
I can download a pdf from the uri of the virtual file, but it's not suitable for importing data.

If anyone knows the answer, a thousand thanks for it!

1 Answers

It depends on what the type of fileId is. (I am not familiar with that particular API).

In general, you need to use ContentResolver to convert the Uri into some usable type such as InputStream, FileDescriptor, etc.

There is also the DocumentFile utility class to work with Document URIs.

Related