Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Best way to convert byte array to Bitmap?

Tags:

java

android

byte

I know why OutOfMemoryError Exception occurs.But there are any best way to convert byte array to Bitmap.And I used below code ,But when large byte it force close app and gives OutOfMemoryError Exception.

And i have API it just return me byte array nothing else.

Bitmap bmp = BitmapFactory.decodeByteArray(bytearray, 0, bytearray.length);
like image 446
Patel Mayur Avatar asked Jul 18 '12 17:07

Patel Mayur


2 Answers

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapbytes , 0, bitmapbytes .length);

Returns The decoded bitmap, or null if the image could not be decode.

like image 117
Zar E Ahmer Avatar answered Sep 18 '22 11:09

Zar E Ahmer


Here is what worked for me: photo is a string of an image by the way.

byte[] imgbytes = Base64.decode(photo, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(imgbytes, 0,
imgbytes.length);
imageupload.setImageBitmap(bitmap);
like image 37
Serdar Samancıoğlu Avatar answered Sep 16 '22 11:09

Serdar Samancıoğlu