2

How can I make a share button in compose, which will share a download link,

I tried this but the result was different that I expected.

Here is the code.

 Button(
            onClick = {
                val intent = Intent(Intent.ACTION_SEND)
                    .putExtra("File Download Link", downloadUrl.value)
                    .setType("text/plain")
                context.startActivity(Intent.createChooser(intent, "Share Using"))
            },

and this is how I am getting the downloadUrl value

val downloadUrl = remember { mutableStateOf("") }
val context = LocalContext.current
val launcher =
    rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri ->
        val task = firebaseUpload(uri, context)
        task.addOnCompleteListener {
            if (task.isSuccessful) {
                downloadUrl.value = task.result.toString()
            }
        }
    }

The downloadUrl value is right but when I copied it in the emulator it was copying the modifier.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Hitesh Patel
  • 183
  • 2
  • 12

1 Answers1

0

you can use this code sample:

onShareClick = {
    val context = LocalContext.current
    val type = "text/plain"
    val subject = "Your subject"
    val extraText = "https://www.google.com/codes/${variable.id}"
    val shareWith = "ShareWith"

    val intent = Intent(Intent.ACTION_SEND)
    intent.type = type
    intent.putExtra(Intent.EXTRA_SUBJECT, subject)
    intent.putExtra(Intent.EXTRA_TEXT, extraText)

    ContextCompat.startActivity(
        context,
        Intent.createChooser(intent, shareWith),
        null
    )
}
buczek
  • 2,011
  • 7
  • 29
  • 40
Hamed Rahimi
  • 144
  • 1
  • 5