Reputation: 3
I'm using the requestForLoadImage
which is an ActivityForResultLauncher<Intent>
built in MainActivity
to get image from gallery.
I've passed the requestForLoadImage
to the productAlertDialog
function in AlertDialog
class.
I'm trying to use a dialog to create a new product. and the user could upload the image of the product from the gallery.
but the problem is I don't know how to use the chosen Image from gallery.
here is the code:
fun productAlertDialog(
context: Context,
requestForLoadImage: ActivityResultLauncher<Intent>,
lifecycleScope: CoroutineScope,
dialogTitle: String,
dialogLayout: Int,
dialog_product_name_container: Int,
dialog_product_price_container: Int,
dialog_product_name: Int,
dialog_product_price: Int,
dialog_product_image: Int,
dialog_product_add_button: Int,
dialog_product_cancel_button: Int,
dialog_product_image_button: Int,
cactusDao: CactusDao
) {
...
val productImage = customLayout.findViewById<ImageView>(dialog_product_image)
...
dialogProductImageButton.setOnClickListener {
if (checkSelfPermission(
context, Manifest.permission.READ_EXTERNAL_STORAGE
) == PermissionChecker.PERMISSION_GRANTED
) {
val galleryIntent =
Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
requestForLoadImage.launch(galleryIntent)
} else {
requestPermissionLauncher.launch(android.Manifest.permission.READ_EXTERNAL_STORAGE)
}
}
dialogProductCancelButton.setOnClickListener {
dialog.dismiss()
}
dialog.show()
}
and here is the requestForLoadImage
in the MainActivity
:
val requestForLoadImage = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK && result.data != null) {
val imageUri: Uri = result.data?.data as Uri
}
}
How can i update my productImage
by using imageUri
?
Upvotes: 0
Views: 148