1

When a user clicks a button, i keep switching the background of a layout like this in Activity code:

...
mylayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img1));//On First click
mylayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img2));//On Second Click
mylayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img10));//On 10th Click
...
mylayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img1));//On 11th Click 1st image again and so on.

I have 10 images which i keep rotating.
Soon, it causes OutOfMemory exception. What am i doing wrong?

If it matters in my manifest file i have:

android:minSdkVersion="11"
android:targetSdkVersion="17" />

EDIT 1
Average size of the image is: 50K
Average dimension of the image is: 600x450
All images img1, img2 etc.. are jpeg images

Solution Update
Reducing image dimensions to 300x200 resolved the issue. The memory requirements went down significantly by this single change.

Jasper
  • 8,440
  • 31
  • 92
  • 133
  • when ever you showing the more images in your application you have to handle images using image loader task when you are showing all at a time size exceeds heap size so that outofmemory exception arises – hareesh J Jan 08 '15 at 20:09
  • What size are the images? – Simon Jan 08 '15 at 20:10
  • background drawables means it may be more than 800x1280 images so thats why he is getting out of memory @Simon – hareesh J Jan 08 '15 at 20:13
  • That's what I'm thinking. But, he could be using smaller and upscaling? – Simon Jan 08 '15 at 20:14
  • go through the [Loading Large Bitmaps Efficiently](http://developer.android.com/training/displaying-bitmaps/load-bitmap.html) from developer site for showing large images in efficient manner – hareesh J Jan 08 '15 at 20:16
  • hareesh145> Thanks...Could you pls post your suggestion of reducing image dimensions as a 'Answer' - so that i can accept it as an answer. – Jasper Jan 09 '15 at 08:06

4 Answers4

1

It might be happening because your image resources are in drawable folder; which is equivalent to drawable-mdpi. And your device might be something other than mdpi.

So, either provide images for all the screed densities you are gonna support. Or, put images in drawable-nodpi, so your images will not be resized. Hence no OutOfMemoryException.

Referred from here

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • Thankyou Darpan, yes that reduced the memory footprint somewhat, but not enough. Must try on devices of varying sizes to see its benefits completely. – Jasper Jan 09 '15 at 07:47
0

Use Bitmap.recycle() if you can change your Drawable with Bitmap.

Hope This will help

Sukhbir
  • 1,410
  • 14
  • 33
  • try this Bitmap bit= BitmapFactory.decodeResource(getResources(), R.drawable.large_icon); user_image.setImageBitmap(bit); – Sukhbir Jan 09 '15 at 06:22
0

Each bitmap with dimension 600x450 has size of 600*450*4 = 1 080 000 bytes (1 MB) in RAM. You should load so large bitmaps in another way:

Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.img1);
myLayout.setBackgroundDrawable(new BitmapDrawable(resources, bitmap));

And before changing background of the view you have to do

Drawable background = myLayout.getBackground();
if (background != null && background instanceof BitmapDrawable) {
    myLayout.setBackgroundDrawable(null);
    BitmapDrawable bd = (BitmapDrawable) background;
    bd.getBitmap().recycle();
}
Vladimir Petrakovich
  • 4,184
  • 1
  • 30
  • 46
0

I dont think changing background layout so frequently is a good idead. I suggest that you could use RelativeLayout with an ImageView as first item. Then everytime you need to change your background, you can change your ImageView with some cached library such as: Universal Image Loader

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86