12

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);
2
  • How big is the bytearray? Is really a big image? Perhaps the image is really big for the phone memory and you should compress it on the server-side if you are retrieving it from there. Commented Jan 2, 2013 at 21:57
  • checkout this nice explanation developer.android.com/training/displaying-bitmaps/… Commented May 28, 2014 at 6:22

4 Answers 4

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

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

Sign up to request clarification or add additional context in comments.

Comments

3

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);

Comments

0

You probably have to use this following method (DOC, same method, but with the options parameter):

public static Bitmap decodeByteArray (byte[] data, int offset, int length, BitmapFactory.Options opts)

And play with the options parameter. Hope this will help you =)

Comments

0

You may want to use the AQuery library to load your images, this will help you resize, view etc and avoid the most common memory leaks. This tool can be found here: http://code.google.com/p/android-query/

1 Comment

How could convert byte[] to bitmap with AQuery?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.