2017-10-10 43 views
-2

以字節組如何轉換的ImageView到ByteArray的科特林的Android如何在imageview的科特林

在java中

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

return image 
+2

您可以複製粘貼到Android Studio中的科特林文件,此代碼,它會被轉換爲科特林。 – zsmb13

回答

0

轉換這是用Java科特林轉換器。

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

這可能會幫助你,

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

@BestBest你有沒有試過這個 – UltimateDevil