Skip to content

Commit

Permalink
Let ShareCompat.IntentBuilder create the chooser intent for sharing c…
Browse files Browse the repository at this point in the history
…ontent.

+ The system chooser handles cases where no app is found.
  • Loading branch information
johnjohndoe committed Dec 17, 2021
1 parent ddf8bf6 commit fbd0421
Showing 1 changed file with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,29 +305,21 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
return
}

val sendIntent = ShareCompat.IntentBuilder(context)
val chooserIntent = ShareCompat.IntentBuilder(context)
.setType(mediaMimeType)
.setStream(mediaUri)
.intent

sendShareIntent(context, sendIntent)
.setChooserTitle(R.string.share)
.createChooserIntent()
context.startActivity(chooserIntent)
}

fun shareText(context: Context, text: String) {
val sendIntent = ShareCompat.IntentBuilder(context)
val chooserIntent = ShareCompat.IntentBuilder(context)
.setType("text/plain")
.setText(text)
.intent

sendShareIntent(context, sendIntent)
}

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

private fun appendTimeToFilename(name: String): String {
Expand Down

0 comments on commit fbd0421

Please sign in to comment.