There's 3 Segments that would allow you achieve screenshoting full height of a scrollView.
a) FilePath and AndroidManifest
b) Intent Uri Share
c) BitmapExtensions (Where the actual magic happens)
AndroidManifest.xml
<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/filepaths" />
</provider>xml/filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path
name="shared_images"
path="images/" />
</paths>fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Insert Layouts Here-->
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>MainFragment.xml
private fun shareScreenshotIntent() {
val context = this.context ?: return
val intent = Intent(Intent.ACTION_SEND)
val screenshotUri = this.rootView.getScreenshotUri(this.scrollView.getChildAt(0), "${context.packageName}.fileProvider")
?: return
intent.type = context.contentResolver.getType(screenshotUri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri)
startActivity(Intent.createChooser(intent, "Share"))
}BitmapExtensions.kt
fun Bitmap.getFile(
context: Context,
compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
quality: Int = 100
): File? {
val filename = "${UUID.randomUUID()}"
val storageDir = context.externalCacheDir
val extension = when (compressFormat) {
Bitmap.CompressFormat.JPEG -> ".jpg"
Bitmap.CompressFormat.PNG -> ".png"
Bitmap.CompressFormat.WEBP -> ".webp"
}
val imageFile = File.createTempFile(filename, extension, storageDir)
val outputStream = FileOutputStream(imageFile)
if (compress(compressFormat, quality, outputStream)) {
outputStream.close()
return imageFile
} else {
throw Exception("Unable to compress image to ${compressFormat.name}")
}
}
fun View.getScreenshotUri(
parentViewGroup: View,
manifestProviderAuthorities: String,
compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.PNG,
quality: Int = 100
): Uri? {
val directoryPath = File(context.cacheDir, "images")
if (!directoryPath.exists()) {
directoryPath.mkdirs()
}
val fileExtension: String = when (compressFormat) {
Bitmap.CompressFormat.PNG -> "png"
Bitmap.CompressFormat.JPEG -> "jpg"
Bitmap.CompressFormat.WEBP -> "webp"
else -> "jpg"
}
val stream = FileOutputStream("$directoryPath/screenshot.$fileExtension")
parentViewGroup.getFullScreenshotBitmap().compress(compressFormat, quality, stream)
stream.close()
val imagePath = File(context.cacheDir, "images")
val targetFile = File(imagePath, "${UUID.randomUUID()}_screenshot.$fileExtension")
return FileProvider.getUriForFile(context, manifestProviderAuthorities, targetFile)
}
fun View.getFullScreenshotBitmap(): Bitmap {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val bgDrawable = background
if (bgDrawable != null) {
bgDrawable.draw(canvas)
} else {
canvas.drawColor(Color.WHITE)
}
draw(canvas)
return bitmap
}