Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Read a file without external storage permission

Tags:

android

I don't want my app to require any permissions, but I want the user to be able to select a file for reading. My app doesn't need arbitrary access to the filesystem. However, all openfiledialog implementations I have researched so far seem to assume permission to access external storage.

One workaround I can think of is to configure my app to be among the list of apps to open a certain type of file. I haven't tried this, but I hope this would work without permission to access external storage. However, user guidance would be less then ideal in this case. I would prefer a solution with a dialog and have the user pick the file.

I think this requirement does not undermine security, because the user has full control over the file my app can read. Is this possible somehow?

like image 597
user1785730 Avatar asked May 09 '16 11:05

user1785730


1 Answers

However, all openfiledialog implementations I have researched so far seem to assume permission to access external storage.

Set your minSdkVersion to 19, then use ACTION_OPEN_DOCUMENT, part of the Storage Access Framework.

Or, if you need your minSdKVersion to be below 19, use ACTION_GET_CONTENT on the older devices.

You will get a Uri back via onActivityResult(). Use a ContentResolver and methods like openInputStream() to consume the content identified by that Uri.

I haven't tried this, but I hope this would work without permission to access external storage

Only if you exclude file: Uri values. For example, an <intent-filter> that supports only content: Uri values would work.

like image 64
CommonsWare Avatar answered Sep 19 '22 13:09

CommonsWare