Skip to content

Instantly share code, notes, and snippets.

@root-ansh
Created September 7, 2025 10:55
Show Gist options
  • Save root-ansh/8c84daacc202f8a667f4adc3433b4571 to your computer and use it in GitHub Desktop.
Save root-ansh/8c84daacc202f8a667f4adc3433b4571 to your computer and use it in GitHub Desktop.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- for sharing files that are present in context.cachDir -->
<cache-path
name="shared_cache"
path="." />
</paths>
private fun shareMedia(shareType: RWSMediaType) {
val context = this
when(shareType){
IMAGE -> {
lifecycleScope.launch {
val cachePath = File(context.cacheDir, "images")
cachePath.mkdirs()
val file = File(cachePath, "${System.currentTimeMillis()}.png")
val bitmap = getCurrentImageViewBitmap()
context.saveBitmapToUserSelectedPath(Uri.fromFile(file),bitmap)// convert bitmap to local cache file first
context.shareLocalFile(file)
}
}
PDF ,FILE -> {
val file = if(shareType==PDF) state.pdfFile!! else state.mediaFile!!
context.shareLocalFile(file)
}
}
}
fun Context.shareLocalFile(file: File) {
val context = this
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.extension.lowercase()) ?: "application/octet-stream"
val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = mimeType
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
context.startActivity(
Intent.createChooser(shareIntent, "Share file via")
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment