Skip to content

Instantly share code, notes, and snippets.

@JakeSteam
Last active December 17, 2018 18:57
Show Gist options
  • Save JakeSteam/3a685edee460ad2497fd3827b70622df to your computer and use it in GitHub Desktop.
Save JakeSteam/3a685edee460ad2497fd3827b70622df to your computer and use it in GitHub Desktop.
"Sharing internal / cache images (with text) to other Android apps" (tutorial at https://blog.jakelee.co.uk/sharing-internal-cache-images-with-text-to-other-android-apps)
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="shared_images" path="/images/"/>
</paths>
fun shareImage(file: File) {
val authority = "${BuildConfig.APPLICATION_ID}.fileprovider"
FileProvider.getUriForFile(context, authority, file)?.let {
val shareIntent = Intent()
.setAction(Intent.ACTION_SEND)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.setDataAndType(it, context.contentResolver.getType(it))
.putExtra(Intent.EXTRA_STREAM, it)
.putExtra(Intent.EXTRA_TEXT, "I'm sharing this image!")
context.startActivity(Intent.createChooser(shareIntent, "Share image to:"))
}
}
val imageCache = File(context.cacheDir, "images")
shareImage(File(imageCache, "myimage.png"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment