8

I am using bitmaps. When the code runs it shows an out of memory error. How can the error be avoided. My code follows. Thanks in advance.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); 
img_cook[index].setImageBitmap(myBitmap); 

public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth,
            int reqHeight) {

    Bitmap bm = null;

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
         inSampleSize = Math.round((float)height / (float)reqHeight);    
        } else {
         inSampleSize = Math.round((float)width / (float)reqWidth);    
        }   
       }
1

4 Answers 4

8

When you have done with your Bitmap, means when your Bitmap done its work then make it recyle and null like below:

bitmap.recycle();
bitmap=null;

OR

I think you are downloading Image from url, so I am suggesting you to use Android Query for this, you will never get this error if you used it.

You can download the jar file from here : http://code.google.com/p/android-query/downloads/list Download the jar file and set jar to your Build Path.

 AQuery androidAQuery=new AQuery(this);

As an example to load image directly from url:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

As an example to get Bitmap from url:

androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
    @Override
    public void callback(String url, Bitmap object, AjaxStatus status) {
        super.callback(url, object, status);

        //You will get Bitmap from object.
    }
});

It's very fast and accurate, and using this you can find many more features like Animation when loading; getting a bitmap, if needed; etc.

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

7 Comments

i have downloaded android-query.0.25.10.jar and i am getting error at AjaxStatus. can you help me on this
thnaks its working :) and it's more fast as compare to other code.
Where I have to use this code ? I used this jar file.
Where you need it, just make one method and call this code, pass your image url to this method, that's it.
@pratt I am getting still now this error while loading more than 500 images on scrolling. Any suggestion or setting of any option in AQ.
|
1

Still now your image size are big that why use width and height like that and after set the image the clear the chache

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 60, 60);
  img_cook[index].setImageBitmap(myBitmap);  
  if (myBitmap != null)
   {
     bitmap.recycle();
     bitmap = null;
     System.gc();
   }

1 Comment

gives error "Canvas: trying to use a recycled bitmap "
0

Im guessing you're not getting the OOM exception after you create your first bitmap, but rather, this happens after you load several bitmaps into memory?

Try to improve your efficiency by manually calling recycle() on bitmaps you no longer need. While the GC collects some data of Bitmaps which have all their references freed, the actual memory of the image is stored in native memory, and so a call to bitmap.recycle() is required to release this memory when you need it to be released.

Hope this helps.

Comments

0

Android applications have very low amount of memory. You should manage it carefully to avoid out of memory exception. You can see the Google's Solution

Comments

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.