0

I do

myImage.setBackgroundResource(R.drawable.mydrawable);

and I get an OutOfMemoryError. But the resource is only 5kb !!! I had various stack trace, sometimes not saying anything, sometimes saying I was using around 5Mb and sometimes 7Mb.

This is on the Galaxy S4. On other devices I don't get the error. Is the GC known for being slow to trigger on certain devices ? Any other idea ?

Thomas
  • 8,306
  • 8
  • 53
  • 92
  • Is it a `PNG`? Are you sure *this* is the resource throwing the `OOM`? – nKn Apr 14 '14 at 17:19
  • @nKn it is a `PNG`, and it is when I set it that the error is thrown, sure about that – Thomas Apr 14 '14 at 17:22
  • Would it be possible to convert it to another format such as `JPG`, or you need it to be a `PNG`? One thing is how long is the file on the disk, but another one very different is how much data it takes to the `Bitmap` to represent it, and that's even worse with `PNG`. – nKn Apr 14 '14 at 17:24
  • Maybe this will help: http://stackoverflow.com/questions/12636510/android-out-of-memory-regarding-png-image – nKn Apr 14 '14 at 17:27
  • then set it through XML..my guess is it is not with the image.. – GvSharma Apr 14 '14 at 17:27
  • @nKn the image has some transparent parts unfortunately. But it is just 80x80 pixels – Thomas Apr 14 '14 at 17:27
  • @GVSharma there is a `if` somewhere, I can't do that by xml. Agreed there can be something else fishy, any idea how to inspect that ? – Thomas Apr 14 '14 at 17:28
  • post the log message..... – GvSharma Apr 14 '14 at 17:29
  • This could help too, seems that `PNG` get expanded pretty much in memory and cause an `OOM` even with small images: http://stackoverflow.com/questions/21262919/out-of-memory-heap-size-error-after-adding-10kb-imagebuttons – nKn Apr 14 '14 at 17:29
  • is it all 80x80 or is it just the opaque part, and the transparent one is bigger? – Ivan Bartsov Apr 14 '14 at 17:35
  • 2
    "But the resource is only 5kb !" -- so? If a bridge has a weight limit of 1000kg, and you put 950kg on it, then add 100kg more, and the bridge collapses, you cannot complain that "but the additional weight was only 100kg!". Your problem probably has little to do with this particular resource and more to do with your heap usage overall. Use MAT to determine what is going on with your application's heap. – CommonsWare Apr 14 '14 at 17:44

1 Answers1

0

Try to put your image in a /drawable-nodpi folder instead of /drawable

Distwo
  • 11,569
  • 8
  • 42
  • 65
  • what is `nodpi` for ? – Thomas Apr 14 '14 at 17:23
  • "/drawable" generic folder is considered by the system like "/drawable/mdpi", so when you run the app with hdpi device or more the images are resized, and become too big. – Distwo Apr 14 '14 at 17:23
  • If that doesn't solve your problem, you might have to scale your image down. You can do so using `inSampleSize` of bitmap factory. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Distwo Apr 14 '14 at 17:35