2017-10-10 1 views
-2

Comment convertir imageview à byteArray Kotlin androidComment convertir imageview à byteArray en Kotlin

En 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

Vous pouvez copier coller ce code dans votre fichier Kotlin dans Android Studio il va se convertir à Kotlin. – zsmb13

Répondre

0

Ici, il est utiliser Java pour Kotlin convertisseur.

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

Cela peut vous aider,

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 l'avez-vous essayé? – UltimateDevil