Possible Duplicate:
Android: Strange out of memory issue while loading an image to a Bitmap object
i am downloading images from Url and displaying them. At download time it is giving out of memory error : bitmap size exceeds VM budget
. I am using drawable. Code is below:
HttpClient httpclient= new DefaultHttpClient();
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest);
HttpEntity entity= response.getEntity();
BufferedHttpEntity bufHttpEntity=new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
Bitmap bm = BitmapFactory.decodeStream(instream);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bm,bm.getWidth(),bm.getHeight(), true);
bm.recycle();
BitmapDrawable bt= new BitmapDrawable(useThisBitmap);
System.gc();
Here is the error: 05-28 14:55:47.251: ERROR/AndroidRuntime(4188): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
Use decodeStream(is, outPadding, opts)
with
BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false; //Disable Dithering mode
opts.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
opts.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
opts.inTempStorage=new byte[32 * 1024];
You could check the image size and then downsample it by appropriate factor.
See this question: Handling large Bitmaps
This issue seems to have been reported several times, here and here for instance...
sorry Shalini but if it\'s the same issue, it seems that there is no solution at all...
The only advice of Romain Guy is to use less memory...
So, good luck to think your stuff differently...
Finally, after resample the image as suggested above, you may call bitmap_file.recycle().
The FACT is that there is a BUG on some Android versions, particularly version 2.1 fails all the time with issues like this one.
I released an app in which I have took a lot of care on resource use. I have even deleted a lot of bitmaps that I were using and now they are created on the fly using graphic primitives. I also recycle bitmaps when no using them. And of course I have checked that I have no memory leaks in my app: the used memory does NOT grow without control, it keeps all the time within reasonable values.
Although I have invest a lot of effort on trying to avoid this problem, I continue getting lots of annoying exceptions like that from 2.1 and 2.1-update1 devices. I am using critercism now to report crashes, and I have seen that It occurs even when the app is using just 4 megabytes of RAM, four times less than the 16M of heap size that every Android device must have for an app -and the fact is that most of devices these days have heap sizes bigger than 16M-.
All my bitmaps have a size of 800x480 pixels, that on the worst case as ARGB_8888 may not occupy more than 1.5MB each one, but it crashes trying to load one when having just 4 megabytes occupied, so there should be at least another 12 MB free. And most of my Bitmaps are loaded as ARGB_4444 that occupies a half of memory, I only use ARGB_8888 when the bitmaps looks really poor with 4444.
So for me it is pretty clear that there is something on these Android versions that is not working fine. 99\'9% of this crashes comes from 2.1 and 2.1-update, and the rest may be explained by other punctual reasons.
I\'ve tried many things this is what works.
BitmapFactory.Options opts=new BitmapFactory.Options();
opts.inDither=false; //Disable Dithering mode
opts.inPurgeable=true;
opts.inScale=8;
///after you use your images
System.gc();
This is a practical answer, what I tried to avoid this problem at Run-time. And it also solved my problem.
Runtime.getRuntime().gc();
Calling Garbage Collector is a good Idea.