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);    
        }   
       }
JulianHarty
  • 3,178
  • 3
  • 32
  • 46
Nivas Devarapalli
  • 111
  • 1
  • 2
  • 10

4 Answers4

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.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • i have downloaded android-query.0.25.10.jar and i am getting error at AjaxStatus. can you help me on this – Sanket Kachhela Oct 03 '13 at 07:44
  • thnaks its working :) and it's more fast as compare to other code. – Sanket Kachhela Oct 03 '13 at 07:52
  • Where I have to use this code ? I used this jar file. – Amit Jayaswal Feb 21 '14 at 12:34
  • Where you need it, just make one method and call this code, pass your image url to this method, that's it. – Pratik Dasa Feb 21 '14 at 12:47
  • @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. – Pratik Butani Dec 08 '14 at 10:48
  • Great solution, but it doesn't seem to work when I am using this in loop reusing layouts. Basically a nested for loop. First to reuse a layout and second to insert images to layouts hor. scrollview. It only populates the first layouts hor scrollview. – KasparTr Apr 28 '16 at 23:09
  • Here is the case where I ran into some trouble with this solution. If you have ideas on why, much appreciated. http://stackoverflow.com/questions/36927069/android-aquery-inside-a-loop-to-reuse-layouts – KasparTr Apr 28 '16 at 23:22
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();
   }
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
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.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
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

Mostafa Lavaei
  • 1,960
  • 1
  • 18
  • 27