How to convert imageview to bytearray in kotlin
14,485
Solution 1
Here it is use java to kotlin converter.
val bitmap = (image.getDrawable() as BitmapDrawable).getBitmap()
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream)
val image = stream.toByteArray()
Solution 2
This may help you,
private fun imageToBitmap(image: ImageView): ByteArray {
val bitmap = (image.drawable as BitmapDrawable).bitmap
val stream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream)
return stream.toByteArray()
}
Related videos on Youtube
Author by
Best Best
Updated on June 04, 2022Comments
-
Best Best almost 2 years
How to convert imageview to bytearray kotlin android
In java
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); ByteArrayOutputStream stream=new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); byte[] image=stream.toByteArray(); return image
-
zsmb13 over 6 yearsYou can copy paste this code in your Kotlin file in Android Studio and it will get converted to Kotlin.
-
-
Shubham AgaRwal over 5 yearsNote: AFAIK PNG is lossless and quality parameter has no effect