Open a Google Drive File Content URI after using KitKat Storage Access Framework

Ok. I found that the right way is to use the input stream from the other posts in conjunction with some data from the contentresolver.

For reference here are the hard to find android docs: https://developer.android.com/training/secure-file-sharing/retrieve-info.html

The relevant code to get mimetype, filename, and filesize:

Uri returnUri = returnIntent.getData();
String mimeType = getContentResolver().getType(returnUri);
Cursor returnCursor =
        getContentResolver().query(returnUri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
TextView nameView = (TextView) findViewById(R.id.filename_text);
TextView sizeView = (TextView) findViewById(R.id.filesize_text);
nameView.setText(returnCursor.getString(nameIndex));
sizeView.setText(Long.toString(returnCursor.getLong(sizeIndex)));

And to get the file contents:

getContentResolver().openInputStream(uri)

Hope this helps someone else.

Leave a Comment