0

How can i use ActivityResultContract (Kotlin) in order to pick a photo from the gallery OR to take a photo by the camera using only one button?

jawad
  • 11
  • 1
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 01 '22 at 11:33
  • This is already answered here: https://stackoverflow.com/questions/66441651/activityresultcontracts-way-of-making-the-user-choose-between-gallery-or-camera – Shikhar Deep Sep 30 '22 at 11:19

1 Answers1

3

Pick an image from the gallery:

class TestActivity: AppCompatActivity() {

    private val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
        uri?.let { imageUri ->
            // Suppose you have an ImageView that should contain the image:
            imageView.setImageURI(imageUri)
        }
    }

    private fun onButtonClicked() {
        getContent.launch("image/*")
    }

}

Be sure to use AppCompatActivity()

Mario Huizinga
  • 780
  • 7
  • 14
  • Hi, how can this uri be manipulated? I tried to open the image, the path is content:// How to convert it to file? – Tarsila Costalonga Nov 11 '22 at 13:49
  • It depends on what you want to do with the image. See example in my answer that I have added, to show the image in an ImageView. If you need something else, better ask a new question. – Mario Huizinga Nov 11 '22 at 15:13
  • I want to see the full path of a video I am importing from the gallery. In this uri, I get something like: content://media/external/video/media/173991 but I need /storage/emulated/0/DCIM/Camera/V_20221110_082812.mp4, for instance. How can I figure out this real path? – Tarsila Costalonga Nov 11 '22 at 15:54
  • See this [answer](https://stackoverflow.com/a/49221353/7493938) – Mario Huizinga Nov 11 '22 at 16:15