I was using Storage Access Framework
to access images and using them in my app. I read it somewhere that when using the SAF
, READ_EXTERNAL_STORAGE
permission is not needed to read from internal storage, SD Cards, or anything else. The Android System is the one accessing the storage, so applications do not need direct access and that this is in fact one of the major selling points of the SAF.
But running my app without requesting for the read permission results in crashing of the app and gives an error saying:
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media, requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
Following is the code for building an intent for the same:
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "image/*"
startActivityForResult(intent, READ_STORAGE_REQUEST_CODE)
And here is the code within the onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
callback:
if (data != null) {
avatarImageUri = data.data
image_avatar.setImageURI(avatarImageUri)
avatarImage = File(getPath(context!!, avatarImageUri!!))
isAvatarChanged = true
imageObservable.onNext(isAvatarChanged)
}
I'm simply trying to use an image selected from SAF and set it into the image_avatar
view.
Can anybody help me with this and tell me if SAF actually requires permissions to be provided explicitly?