如何在kotlin中将imageview转换为bytearray

Bes*_*est 0 android imageview kotlin

如何将 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
Run Code Online (Sandbox Code Playgroud)

Muz*_*ain 13

这里是使用 java to kotlin 转换器。

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

  • 注意:AFAIK PNG 是无损的,质量参数没有影响 (2认同)