如何将imageview转换为kotlin中的bytearray

如何将imageview转换为bytearray kotlin android

在java中

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); ByteArrayOutputStream stream=new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); byte[] image=stream.toByteArray(); return image 

这里使用java来转换kotlin。

 val bitmap = (image.getDrawable() as BitmapDrawable).getBitmap() val stream = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream) val image = stream.toByteArray() 

这可能会帮助你,

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