3

I require displaying many images in my application. These being jpgs and pngs and i'm loading them inside ImageViews like so:

tile.setImageResource(R.drawable.tile_highlight);

I am currently having OutOfMemory problems (java.lang.OutOfMemoryError: bitmap size exceeds VM budget)

I've searched and found some other posts, they all suggest that you should recycle the bitmap of an ImageView manually, like so: ((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle(); which will dump it from memory.

BUT in my case, being that i'm not using setBitmap() to load the images onto the ImageView objects, when i try and run the above code, it returns NullPointerException, more precisely, the method getBitmap() returns null, there is no bitmap ?!?!

Do i need to go back in my code and change the way i load all the images in the ImageViews, and then try with the recycle() method? Or how can i free up the memory so it doesn't crash anymore?

EDIT

I've tried something like so: imageView.setImageResource(-1); in hopes it will remove the image from memory and replace it with ... null or something, but it seems it doesn't help the cause.

AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106
  • you can put catch for OutOfMemoryError and in catch you can resize it and again set to imageview – rajpara Jul 18 '12 at 14:15
  • Look here http://developer.android.com/reference/android/widget/ImageView.html#setImageResource(int) Consider using setImageDrawable and see if it helps. Drawable image = context.getResources().getDrawable(R.drawable.image); ImageView.setImageDrawable(image); – Maxim Jul 18 '12 at 14:17
  • 2
    possible duplicate of [Out of Memory Error ImageView issue](http://stackoverflow.com/questions/10200256/out-of-memory-error-imageview-issue) – rds Jul 30 '15 at 09:24

5 Answers5

4

It would be helpful if you could post some of your code. Specifically, how you're setting the images of the ImageView objects. If you're not using bitmaps, I would expect that getBitmap() will return null. However, if you're using another sort of Drawable or otherwise to set the image, there's likely a similar route to take that doesn't involve bitmaps.

EDIT:

Alright, give this a shot. You can create a Bitmap from a resource like this:

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

Then, use the bitmap like this, considering img is your ImageView:

img.setImageBitmap(bm);
//Do some stuff with it, then when memory gets low:
((BitmapDrawable)img.getDrawable()).getBitmap().recycle();

Of course, this is considering that you're in an activity. If not, you'll have to get a handle on the context and replace getResources() with context.getResources().

flakes
  • 21,558
  • 8
  • 41
  • 88
boztalay
  • 562
  • 6
  • 17
  • I'm just using setImageResource(R.drawable.someImage) to load the images inside the ImageViews – AndreiBogdan Jul 18 '12 at 14:22
  • The code from above will not fix it. You can meet this issue if you have some memory leaks in your application. Bitmap recycling can help you but it will blind solution. In normal cases you should not use Bitmap.recycle() native method. Try to analyze your HPROF using DDMS and hprof-conv from android SDK tools. – ddmytrenko Sep 04 '13 at 10:43
2

If you want to set an image from drawable to ImageView, Do not use ImageView.setImageResource(int resId) directly with drawable id. Instead get Scaled down bitmap(if applicable) and set it to imageView. like this: this works for me.

    iv.setImageBitmap(decodeResource(getResources(), R.drawable.big_image));

    private static Bitmap decodeResource(Resources res, int id) {
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
            try {
                bitmap = BitmapFactory.decodeResource(res, id, options);
                Log.d(TAG_LOG, "Decoded successfully for sampleSize " + options.inSampleSize);
                break;
            } catch (OutOfMemoryError outOfMemoryError) {
                // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
                Log.e(TAG_LOG, "outOfMemoryError while reading file for sampleSize " + options.inSampleSize
        + " retrying with higher value");
            }
        }
        return bitmap;
    }

source: https://codingjunkiesforum.wordpress.com/2014/06/12/outofmemory-due-to-large-bitmap-handling-in-android/

ElOjcar
  • 301
  • 2
  • 4
  • 12
Solivan
  • 695
  • 1
  • 8
  • 16
1

try

((BitmapDrawable)im.getBackground()).getBitmap().recycle();
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
1

I think you should display images with lower resolution. This would resolve the OOM problem. You can also read Android Developer Guideline link

A-Shi
  • 31
  • 3
1

OutOfMemory error comes when you dont free bitmap once its being used(may be large size) so to prevent this we should keep in mind of re-sizing,taking weakreferences etc. but there are good libraries available which is taking care all problem one of them is Picasso. please have a look at-

loading images using Picasso

Godfather
  • 833
  • 9
  • 14