Skip to content

Instantly share code, notes, and snippets.

@dmersiyanov
Last active May 25, 2022 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmersiyanov/5a4381c559636541c83de47cf3f2319e to your computer and use it in GitHub Desktop.
Save dmersiyanov/5a4381c559636541c83de47cf3f2319e to your computer and use it in GitHub Desktop.
Android sharing with image preview
<paths>
<cache-path
name="shared_images"
path="images/" />
</paths>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="appPackage">
<application
android:name=".app.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
<activity
android:name=".ui.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Launcher"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<provider
android:authorities="appPackage.fileprovider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
fun Context.shareWithImagePreview(text: String, previewTitle: String) {
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/plain"
sharingIntent.putExtra(Intent.EXTRA_TEXT, text)
sharingIntent.putExtra(Intent.EXTRA_TITLE, previewTitle)
val previewImageClipData: ClipData? = getClipDataThumbnail(this)
previewImageClipData?.let {
sharingIntent.clipData = it
sharingIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
startActivity(Intent.createChooser(sharingIntent, null))
}
private fun getClipDataThumbnail(context: Context): ClipData? {
return try {
val contentUri = saveThumbnailImage(context)
ClipData.newUri(context.contentResolver, null, contentUri)
} catch (e: FileNotFoundException) {
Timber.e(e.localizedMessage ?: "getClipDataThumbnail FileNotFoundException")
null
} catch (e: IOException) {
Timber.e(e.localizedMessage ?: "getClipDataThumbnail IOException")
null
}
}
fun saveThumbnailImage(context: Context): Uri? {
// Domain authority for our app FileProvider
val fileProviderAuthority = "appPackage.fileprovider"
// Cache directory to store images
// This is the same path specified in the @xml/file_paths and accessed from the AndroidManifest
val imageCacheDir = "images"
// Name of the file to use for the thumbnail image
val imageFile = "thumbnail.png"
val bm = BitmapFactory.decodeResource(context.resources, R.drawable.any_icon)
val cachePath = File(context.applicationContext.cacheDir, imageCacheDir)
cachePath.mkdirs()
val stream = FileOutputStream("$cachePath/$imageFile")
bm.compress(Bitmap.CompressFormat.PNG, 100, stream)
stream.close()
val imagePath = File(context.cacheDir, imageCacheDir)
val newFile = File(imagePath, imageFile)
return FileProvider.getUriForFile(context, fileProviderAuthority, newFile)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment