Transform image into array of bytes

-1

I have an image that is in ImageView on Android and I need to convert the image to byte array to send to MySQL. How could I do it?

    
asked by anonymous 16.10.2017 / 02:28

1 answer

1

Use this method:

public byte[] convertImageViewToByteArray(ImageView image){
    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    return stream.toByteArray();
}

Note: The method assumes that the image was assigned to ImageView with android:src . If you were android:background instead of

image.getDrawable()

use

image.getBackground()
    
16.10.2017 / 15:38