Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ShareCompat.IntentBuilder for sharing content #4745

Merged
merged 3 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4745.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Open share UI provides by the system when sharing media or text.
Original file line number Diff line number Diff line change
Expand Up @@ -305,26 +305,28 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
return
}

val sendIntent = ShareCompat.IntentBuilder(context)
val chooserIntent = ShareCompat.IntentBuilder(context)
.setType(mediaMimeType)
.setStream(mediaUri)
.getIntent()
.setChooserTitle(R.string.share)
.createChooserIntent()

sendShareIntent(context, sendIntent)
createChooser(context, chooserIntent)
}

fun shareText(context: Context, text: String) {
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.type = "text/plain"
sendIntent.putExtra(Intent.EXTRA_TEXT, text)
val chooserIntent = ShareCompat.IntentBuilder(context)
.setType("text/plain")
.setText(text)
.setChooserTitle(R.string.share)
.createChooserIntent()

sendShareIntent(context, sendIntent)
createChooser(context, chooserIntent)
}

private fun sendShareIntent(context: Context, intent: Intent) {
private fun createChooser(context: Context, intent: Intent) {
try {
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share)))
context.startActivity(intent)
} catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(R.string.error_no_external_application_found)
}
Expand Down