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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
}
}
}
}