How to convert imageview to bytearray in kotlin

橙三吉。 提交于 2020-08-06 07:19:11

问题


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

回答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()



回答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()
}


来源:https://stackoverflow.com/questions/46666308/how-to-convert-imageview-to-bytearray-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!