0

I am trying to place an image as background in my TextView. I want the image to get changed by pressing a button. At the first image gets loaded but when I try for next time it says OutOfMemoryError.

My Code:

int [] Quo = {R.drawable.wallpaper10,R.drawable.wallpaper2,R.drawable.wallpaper3,R.drawable.wallpaper4};
txtView.setBackgroundResource(Quo[j] );

It works perfectly if i give

txtView.setBackgroundResource(Quo[0]);

I am new to android development please help me resolve issue.

kenju
  • 5,866
  • 1
  • 41
  • 41

1 Answers1

-1

image memory usage is based on multiple things:

  1. resolution of the image.
  2. where the image file is located (the density qualifier of the drawable folder) compared to on which screen density the app is running on.
  3. which bitmap format you used .
  4. whether or not the image is 9-patch image.

so , for example, if you have a 100x100 image, and the file is in "drawable-mdpi" yet it runs on an xhdpi device , and the bitmap format is the default one, and it's not a 9-patch image, the bitmap would take:

100*100 * 4 *4 = 160,000 bytes.

the 100*100 is because of the number of pixels for width and height.

the first 4 is because on the default bitmap format, each pixel takes 4 bytes.

the second 4 is because the density is 2 times larger in both width and height (2*2=4).

so, if this is the only code you show, i think you use too much memory because of your images.

one thing that you could do is to downsample them.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Thanks a lot size of the image is the problem when i used low resolution image it worked.Thanks. – Archanaa Shiva Apr 03 '13 at 00:39
  • In general, if an answer has helped you on stackOverflow, the one who asked the question ticks the answer as answered, by clicking on the "V" button – android developer Apr 03 '13 at 18:14
  • You improved your writing quality for quite a bit, @androiddeveloper, but things seem to be going downhill again. Stack Overflow is neither a chatroom nor a text message. Please read [this reminder](http://meta.stackexchange.com/questions/165504/can-a-literate-user-be-encouraged-to-stop-deliberate-writing-errors) from the community. – halfer Apr 06 '13 at 11:53
  • I've already told you that I don't always have the time to review things I write using third party apps. The main thing here is helping each other and not arguing with non-english people about how they should write. My main language is not english, so I can't always fix myself in such an automatic way as english-people can. If you wish, I can help you with hebrew, but I will never bash you about your mistakes (typos i don't like though, you have no idea how many people make mistakes on those on hebrew). – android developer Apr 06 '13 at 12:11
  • Anyway, It's very hard for me to keep fixing sentences I write in, and I prefer helping people with things that i'm good at instead. I hope that other people either understand it or just ignore the differences. – android developer Apr 06 '13 at 12:26
  • Unfortunately, the community disagrees with you, @androiddeveloper. Summary for other readers: **your English is nearly perfect**, but you are happy for many other contributors to carry on fixing your deliberate errors; when the community asks you to make the slightest bit of extra effort, you dig your heels in; you've taken advantage of the fact that there is little appetite to moderate the dreadful standards of the [Android] tag; and you've declined the opportunity to respond to my plea to improving standards on Meta. Good work! – halfer Apr 08 '13 at 19:08
  • Well this is tolerance to other people. I get to handle it everywhere. People have their accent. People constantly make their Hebrew mistakes and typos. I never ignore or make fun of them for this, and as long as I understand them (and I do tell them from time to time about their mistakes), I think it's OK. You need to learn this that in the real world, there are people. Many people find it hard to change habits and learn some types of things. I think I've made it clear that I am trying my best. Whoever disagrees with me just says there is no place for people with those kinds of difficulties. – android developer Apr 08 '13 at 21:48