Transforming image into array of bytes

Asked

Viewed 843 times

0

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

  • Where the image came from?

  • Image uploaded from device to Imageview

1 answer

2

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 it was with android:background instead of

image.getDrawable()

use

image.getBackground()
  • In case I’m taking an image of the device and setting in Imageview, the process will be the same?

  • What do you mean? Explain it better.

  • I have an Imageview and when I click on it I take an image from the device’s gallery and press it from there by clicking on the save button it sends to the database

  • I still don’t understand. If you want to convert the image that is in Imageview to Byte[] use the answer method. If you have another question create a new one with the necessary details .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.