-2

I use the function setBackgroundResource(R.drawable.image) a lot.

I have like 7 buttons and want to change the background when one is on focus. Using a selector I guess wouldn't work as the user could tap on an other view and none of my buttons would be on focus.

Problem : I get a OutOfMemoryError here :

        but.setBackgroundResource(resIdNew);

        current_b_selection.setBackgroundResource(id_res_back);

I think my way to do it is just wrong, but I don't really get what's the good practice here.

Sebastien FERRAND
  • 2,110
  • 2
  • 30
  • 62

2 Answers2

2

The error is self explanatory, your Image or images are too large, try something with a maximum of 10kb. This will help you save the memory. This error is common if you are testing on emulator, If so then go to that specific emulator device on device manager and click edit then Increase the RAM, and also internal memory if necessary and also the heap size. Well as for me I would just do my testing first on a real device, if the same issue persists then I would have to review the size of my images.

Kevin Kaburu
  • 504
  • 4
  • 19
  • `maximum of 10kb`- so you mean an image of about 50 pixels square? File size has nothing to do with how much memory is required. – Simon Feb 10 '14 at 09:17
  • 1
    I think file size always relies on the number of pixels. Just work on lowering the pixels to something reasonable. what is the size of your images in terms of pixels? – Kevin Kaburu Feb 10 '14 at 09:23
  • Smaller image nailed it ! thanks – Sebastien FERRAND Feb 10 '14 at 09:49
  • 1
    @KevinKaburu Nope. It relies on the compression scheme. Create a 50x50 image with s single colour, then save as PNG, JPEG and BMP. Now compare file sizes. Now change one pixel and save again. File size has a relationship to pixels but does not "rely" on it. Memory required is widthxheightx4 bytes if using ARGB888 and apart from decompression, is not related to file size. – Simon Feb 10 '14 at 13:35
  • @Simon I dint know that :-/ . It is good you pointed that one out to me. Though I hope the solution worked for you, and if it did please vote the answer so as it can help more developers out there... – Kevin Kaburu Feb 10 '14 at 13:39
  • @SebastienFERRAND If it indeed helped the vote the answer so as we can all help the community – Kevin Kaburu Feb 10 '14 at 13:39
0

Try this:

In the application tag of your manifest file, add an attribute:

android:largeHeap="true". See if it removes the OutOfMemoryError or not...

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174