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?
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?
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()