RealBadCoder RealBadCoder - 1 year ago 259
Android Question

How Can I convert an imageview to byte array in Android Studio?

My goal is to get a picture selected by the user and populate an image view with it. Then, on clicking a button, that image will be sent to a Parse database. I know I have to convert the imageview to byte array, but Doesn't seem to work.

Any help will be highly appreciated.
here's my code:

//send the imageviwew to parse database

public void sendToParseBtn (View view){
Bitmap bitmapImage = findViewById (;
ImageView imageView = (ImageView) findViewById(;

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 40, stream);

byte[] byteArray = stream.toByteArray();

ParseFile file = new ParseFile("an.jpg",byteArray);
ParseObject object = new ParseObject("MyParseObject");
object.put("imageFile", file);


Answer Source

Try converting the image view to a bitmap drawable first then get the byteArray:

ImageView imageView = (ImageView) findViewById(;
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageInByte = baos.toByteArray();
//save your stuff
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download