0

I know this question has been asked so many times here. I have gone through all that I came across, and unfortunately all of them said the same answer.

My requirement: I need to convert huge byte array to bitmap. By huge I meant, I am sending an http request to fetch an image, the image is fetched as byte array, I need to display this image in my application in an image view. The problem is, the image I need to fetch can be of any size, ranging from 100kb to 10+mb. The following is the solution I found all over the internet for converting byte array to bitmap in android.

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata.length); //bitmapdata is the byte array

This works perfectly for small images, but as the image size increases to say from 2-3mb onwards, this will crash with an out of memory error. Is there a better way for the bitmap conversion which won't crash my app?

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113

1 Answers1

0

Heavy bitmaps usually mean very large bitmaps. You can limit the size of a resulting bitmap from a byte array (or input stream) by using Options with the BitmapFactory:

Options options = new Options();
options.outWidth = widthLimit;
options.outHeight = heightLimit;
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);

This should get you started.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
  • What if I need the bitmap in full resolution without down sizing? – Harikrishnan Aug 11 '14 at 06:30
  • There's no point to have a higher resolution than your screen size. Try limiting it to that? If you're trying to display a truly massive picture which the user should be able to scroll, then you should perhaps try methods like splitting the image up into segments, or even look at rendering it using OpenGL-ES textures. – Gil Moshayof Aug 11 '14 at 06:32
  • Let me try this one. :) – Harikrishnan Aug 11 '14 at 06:34
  • I know I was testing the extremes, but this caused the same crash when I tested with an image of 16mb in size. I may receive images of any size, mostly heavily sized images like this in my app. – Harikrishnan Aug 11 '14 at 07:15