Search code examples
androidrelative-pathpermission-deniedandroid-contentresolverstorage-access-framework

Android open InputStream from relative uri to ACTION_OPEN_DOCUMENT?


I'am trying to open multiple files relative to a json configuration file in android 4.4 (api level 19), I used

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType({mime});
this.startActivityForResult(intent, {code});

to let the user find te configuration file, and from there open multiple files that I know the path from the config file.

But i get

Caused by: java.lang.SecurityException: 
Permission Denial: reading com.android.providers.downloads.DownloadStorageProvider
uri content://com.android.providers.downloads.documents/document/raw:/storage/emulated/0/Download/{relative file}
requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

I dont want to use user interfaces to open the other files, so I tried adding the following permissions without getting any results

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS "/>

I would rather have the files separated and not in a blob or a zip file


Solution

  • ACTION_OPEN_DOCUMENT only gives you access to exactly the file (or files, if you use EXTRA_ALLOW_MULTIPLE) that the user selects.

    You can use ACTION_OPEN_DOCUMENT_TREE to allow the user to select a folder - you'll then get access to all files in that folder (and their subfolders).