Code: Select all
private fun showNativeShareDialog(contentUri: Uri) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "image/*"
putExtra(Intent.EXTRA_STREAM, contentUri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val chooserIntent = Intent.createChooser(intent, "Compartir imagen")
shareResultLauncher.launch(chooserIntent)
}
private val shareResultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
val chosenComponent = result.data?.getStringExtra(Intent.EXTRA_CHOSEN_COMPONENT)
chosenComponent?.let { packageName ->
when {
packageName.contains("whatsapp") -> {
// Usuario eligió WhatsApp
}
packageName.contains("instagram") -> {
// Usuario eligió Instagram
}
// etc...
}
}
}
}