See Sharp
See Sharp

Reputation: 1

How to convert byteArray into bitmap image to show in an imageview in Android?

I have the following code for this purpose in my app but it does nothing, and halts the application:

bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length));

Upvotes: 0

Views: 441

Answers (4)

moDev
moDev

Reputation: 5258

Try this :

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 95, blob);
byte[] bitmapdata = blob.toByteArray();

Bitmap bmp = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
ImageView image = (ImageView) findViewById(R.id.ivPhoto);
image.setImageBitmap(bmp);

Upvotes: 0

AndroidEnthusiastic
AndroidEnthusiastic

Reputation: 931

use below line to convert bytes into Bitmap, it is working for me.

  Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

you need to put above line outside of loop, as it takes Bytes Array and convert into Bitmap.

P.S. :- here imageData is bytes array of Image

Upvotes: 0

g00dy
g00dy

Reputation: 6778

Then I suppose you draw the image too like this:

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bmImage, 0, 0, paint);

Upvotes: 0

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

Try like this

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

Upvotes: 1

Related Questions