Last active
September 6, 2023 14:11
-
-
Save chiragthummar/26838eec16966a7a10cccc4741444cd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun createBitmapFromPicture(picture: Picture): Bitmap { | |
val bitmap = Bitmap.createBitmap( | |
picture.width, | |
picture.height, | |
Bitmap.Config.ARGB_8888 | |
) | |
val canvas = android.graphics.Canvas(bitmap) | |
canvas.drawColor(android.graphics.Color.WHITE) | |
canvas.drawPicture(picture) | |
return bitmap | |
} | |
private suspend fun Bitmap.saveToDisk(context: Context): Uri { | |
val file = File( | |
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), | |
"screenshot-${System.currentTimeMillis()}.png" | |
) | |
file.writeBitmap(this, Bitmap.CompressFormat.PNG, 100) | |
return scanFilePath(context, file.path) ?: throw Exception("File could not be saved") | |
} | |
private fun File.writeBitmap(bitmap: Bitmap, format: Bitmap.CompressFormat, quality: Int) { | |
outputStream().use { out -> | |
bitmap.compress(format, quality, out) | |
out.flush() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment