Take All Persistable Uri Permissions At A Time If You Want Them To Last Over Reboot
If I don't get it wrong, declaring for the android.permission.WRITE_EXTERNAL_STORAGE
permission is bundled with android.permission.READ_EXTERNAL_STORAGE
, so in old ways if I am a lazy guy, I just don't have to write the permission declaration for the later one.
But time has changed when Google's new policy is asking developers to use Storage Access Framework, when taking the persistable uri permissions, you must take reading and writing permission both at one. Other wise you won't be able to read or write after reboot. To do so you can use the following kotlin codes:
class StoragePermissionFragment : Fragment(){
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == OPEN_DIRECTORY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
val directoryUri = data?.data ?: return
activity?.contentResolver?.takePersistableUriPermission(
directoryUri,
// take both permissions at a time by combining flags with `or` operator
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
or Intent.FLAG_GRANT_READ_URI_PERMISSION
)
} else if (resultCode == Activity.RESULT_CANCELED) {
// handle canceled
}
}
}
}