share intent in android
Here is an example of how to create different types of share intents in
Android using Kotlin. Please note that you will need to replace the
placeholders such as SHARE_TITLE
and
SHARE_TEXT
with the actual values you want to share.
- Share plain text:
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_SUBJECT, "SHARE_TITLE")
intent.putExtra(Intent.EXTRA_TEXT, "SHARE_TEXT")
startActivity(Intent.createChooser(intent, "Share via"))
- Share an image:
val intent = Intent(Intent.ACTION_SEND)
intent.type = "image/*"
val imageUri = Uri.parse("PATH_TO_IMAGE_FILE")
intent.putExtra(Intent.EXTRA_STREAM, imageUri)
intent.putExtra(Intent.EXTRA_TEXT, "SHARE_TEXT")
startActivity(Intent.createChooser(intent, "Share via"))
- Share a video:
val intent = Intent(Intent.ACTION_SEND)
intent.type = "video/*"
val videoUri = Uri.parse("PATH_TO_VIDEO_FILE")
intent.putExtra(Intent.EXTRA_STREAM, videoUri)
intent.putExtra(Intent.EXTRA_TEXT, "SHARE_TEXT")
startActivity(Intent.createChooser(intent, "Share via"))
- Share a file:
val intent = Intent(Intent.ACTION_SEND)
intent.type = "*/*"
val fileUri = Uri.parse("PATH_TO_FILE")
intent.putExtra(Intent.EXTRA_STREAM, fileUri)
intent.putExtra(Intent.EXTRA_TEXT, "SHARE_TEXT")
startActivity(Intent.createChooser(intent, "Share via"))
- Share multiple files:
val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
intent.type = "*/*"
val fileUris = arrayListOf()
fileUris.add(Uri.parse("PATH_TO_FILE1"))
fileUris.add(Uri.parse("PATH_TO_FILE2"))
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris)
intent.putExtra(Intent.EXTRA_TEXT, "SHARE_TEXT")
startActivity(Intent.createChooser(intent, "Share via"))
In all of these examples, the
startActivity(Intent.createChooser(intent, "Share via"))
method
shows the chooser dialog to the user, allowing them to select the app they
want to share the content with.