Junior — Middle
How can you get a link to an image selected from the device's gallery?
sobes.tech AI
Answer from AI
To obtain a URI link to an image selected from the device gallery in Android, an Intent with the action Intent.ACTION_PICK or Intent.ACTION_GET_CONTENT is usually used. After selecting the image, you can get the URI of the selected image from the data Intent in the onActivityResult method.
Example:
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == Activity.RESULT_OK) {
val imageUri: Uri? = data?.data
// imageUri is a link to the selected image
}
}
The URI can be used to display the image or for further processing.