Search code examples
androidgalleryandroid-permissionsandroid-galleryuser-permissions

Fetching images from gallery- able to fetch without any permission


Using below code to fetch images from gallery

val intent = Intent()
intent.action = Intent.ACTION_PICK
intent.type = "image/*"
if (multiImageAllowed) intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
context.startActivityForResult(intent, PICK_IMAGE_GALLERY)

When i use this intent, it show me chooser with multiple apps, i select one of the app and get an image, and show it in my app,(i get the image in onActivityResult), these app have their own runtime permission checks. Do i need to add gallery permission check in my app too ? if it's required then why android is not stopping me or giving an exception. Is it just a good convention to ask user for permission when he/she intentionally opening an app to add images ?

so my question is , should i add runtime permission in my app too or is it not required.(since those apps are handling it anyway, and i can not access more image than the one he/she selects)


Solution

  • using below code to fetch images from gallery

    If you wish to have the user pick content based on MIME type, use ACTION_GET_CONTENT or ACTION_OPEN_DOCUMENT. ACTION_PICK is to have the user pick content based on a particular collection, identified by a Uri in the Intent. This is why the ACTION_PICK documentation mentions that the input is a Uri and does not mention anything about MIME type.

    Do i need to add gallery permission check in my app too ?

    I am guessing that you are referring to READ_EXTERNAL_STORAGE. In theory, a Uri returned by ACTION_PICK, ACTION_GET_CONTENT, or ACTION_OPEN_DOCUMENT should be usable without additional permissions, at least for a short time.

    However, not everybody plays by the rules, so IMHO it is safest if you hold READ_EXTERNAL_STORAGE. If you are concerned about asking your users for that permission, you could try delaying it — try using the Uri, and if you get a SecurityException, then ask for the runtime permission. Then try using the Uri again if the user grants that permission. Personally, I would aim for better reliability and would request READ_EXTERNAL_STORAGE first.