Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte array of image into imageview

I've searched a bit but can't get a clear glimpse of it. How can I set a byte array of an Image into an ImageView? I tried with this but it didn't work.

BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));
like image 337
Manoj Kumar Avatar asked Dec 13 '12 07:12

Manoj Kumar


1 Answers

This is how you can convert a Bitmap to a ByteArray and a ByteArray to a Bitmap:


Convert Bitmap to ByteArray:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); 

Convert ByteArray to Bitmap:

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); ImageView image = (ImageView) findViewById(R.id.imageView1); image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false)); 
like image 184
Dipak Keshariya Avatar answered Sep 19 '22 13:09

Dipak Keshariya