0

I am working on a wallpaper app. I have all my images stored in Drawable folder.

I am getting OutOfMemoryError when I add more than 30 wallpapers. I want to know how to overcome this.

Should I save all my images in SQLite Database and then load from there or should I have to do something else.

I have tried resizing bitmaps through Decode Bitmap Factory but it did not serve my purpose as the quality of images is reduced.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
Faisal I
  • 1
  • 5

3 Answers3

1

Resizing images at decode-time will only move your OutOfMemoryError ahead in time. That error means that your application leaked memory, or tried to use more memory than the available one. When working on bitmaps on Android, this happens quite often, because the limit is set around 25MB, and high resolution screens are increasingly common.

You have to redesign your application. There's no need to keep 30 images in memory, because they can't fit in a single screen - well, if they are thumbnail-size, you resize them all when you decode, and the total number of pixels in memory is the same as a single big picture, so you don't run out of memory.

You have to find a way to recycle() bitmaps when they are not visible. If you better describe your application, we can help you find the appropriate moment, also to preload images to have a responsive application and a better user experience.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • Actually in my app i am displaying the categories of wallpapers on main page then if a user clicks a particular category then all the wallpapers in that category are displayed in a scroll view and user selects for a particular wallpaper to be his wallpaper. – Faisal I Dec 29 '12 at 06:25
  • When i run my app on Emulator it crashes and in Logcat it says OutOfMemory Exception. When i reduce the number of images than it runs perfectly and i don't get any error. – Faisal I Dec 31 '12 at 06:44
  • I mean what triggers the error. Viewing the categories thumbs? – Raffaele Dec 31 '12 at 08:07
  • How many category are there, and how big are their thumbnails? Also, you should post the relevant code – Raffaele Dec 31 '12 at 11:47
0

I think what you need to do is display Thumbnails instead of drawable image on your screen. You can generate Thumbnails and display as per your size requirements. And whenever user click on Thumb, just take original path and set wallpaper.

Another option is you can use Universal Image Loader which helps you to buffer your image in disc (like SD card or your application's Internal memory). So issue of Out of Memory can be resolved.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
-1

If you want to control the crash of your Application then write your code in Exception block:

try {

    ...

    }
    catch(OutOfMemoryError error)  {
        //decide what to do when there is not more memory available
    }

Also please the this link:

link

Community
  • 1
  • 1
Sam-In-TechValens
  • 2,501
  • 4
  • 34
  • 67