I have an activity in my app that allows the user to select several files from the device one by one, I am using an intent like this:
Intent intent = new Int
Writing ACTION_OPEN_DOCUMENT is not helping entirely. After a restart your URI Permission is gone even with this action.
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
intent.setAction(Intent.ACTION_GET_CONTENT);
} else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
To complete Alécio Carvalho's answer look at this documentation on the same page. There is a good explanation regarding URI Permissions and how to make them persist.
Just add this to your onActivityResult method and your permission persists through a device restart. (Tested with a Google Pixel.)
override fun onActivityResult(requestCode:Int, resultCode:Int, intent:Intent?) {
if(requestCode == SELECT_PICTURE && intent != null){
val uri = intent.getData()
val takeFlags = intent.getFlags() and (Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
activity.getContentResolver().takePersistableUriPermission(uri, takeFlags)
PreferenceHelper.setHeaderImageUri(activity, uri.toString())
}
}
(sorry kotlin lover...java version is inside the link)