Search code examples
androidkotlinandroid-intentandroid-galleryandroid-contentresolver

Trying to select image from gallery on a different user profile


We are currently working on an android app that is running in a managed device enterprise environment.

Right now we try to pick an image via intent by using the following code:

val galleryPickerIntent = Intent(Intent.ACTION_PICK)
galleryPickerIntent.type = "image/*"
galleryPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)

fragment.startActivityForResult(galleryPickerIntent, SELECT_IMAGES_INTENT_RESULT)

Afterward, we process the images via contentResolver from the intent.clipData which gives us an URL like this: /storage/emulated/0/DCIM/Camera/IMG_20200130_113056.jpg

When we try to access this URL from a different profile, for example, the app is running in the work profile but the gallery app which was used to select is in the private profile then trying to access this image URL will result in access denied exception.

Is there a proper way to solve this problem? Ideally, we would allow work profile & private apps to be used for image selection.


Solution

  • I found the answer, we we're resolving the image incorrectly. We extracted the filePath from the uri instead of using contentResolver.openInputStream directly on it that solved our problem.