Reputation: 21
I'm facing an OutOfMemory Error on my application.. the reason is I have lots of images loaded on my application..So upon researching for possible solutions, I found this link and downloaded the whole sample project http://developer.android.com/training/displaying-bitmaps/index.html
I find it related to my problem only that it loads the images online which is in my case, all the images in my application can be found on drawables..
So my question is, is it possible to use the images from my drawables rather than loading it from specific URL. If it is, please kindly give me a very simple sample of it....
I'm really having a hard time on this since Im new to android development and Im just relying on tutorials online.. thanks
Here is what I am talking about...
public class Images {
/**
* This are PicasaWeb URLs and could potentially change. Ideally the PicasaWeb API should be
* used to fetch the URLs.
*
* Credit to Romain Guy for the photos:
* http://www.curious-creature.org/
* https://plus.google.com/109538161516040592207/about
* http://www.flickr.com/photos/romainguy
*/
public final static String[] imageUrls = new String[] {
"https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
"https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg",
"https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg",
"https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg",
// more …
"https://lh4.googleusercontent.com/-e9NHZ5k5MSs/URqvMIBZjtI/AAAAAAAAAbs/1fV810rDNfQ/s1024/Yosemite%252520Tree.jpg",
};
/**
* This are PicasaWeb thumbnail URLs and could potentially change. Ideally the PicasaWeb API
* should be used to fetch the URLs.
*
* Credit to Romain Guy for the photos:
* http://www.curious-creature.org/
* https://plus.google.com/109538161516040592207/about
* http://www.flickr.com/photos/romainguy
*/
public final static String[] imageThumbUrls = new String[] {
"https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s160-c/A%252520Photographer.jpg",
"https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
"https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg",
"https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg",
"https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg",
"https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg",
// … more
"https://lh4.googleusercontent.com/-e9NHZ5k5MSs/URqvMIBZjtI/AAAAAAAAAbs/1fV810rDNfQ/s160-c/Yosemite%252520Tree.jpg",
};
}
Note: this code was the sample provided on this link http://developer.android.com/training/displaying-bitmaps/index.html
Im sorry if my explanation is not clear enough, feel free to ask me for follow up question... thanks
Upvotes: 0
Views: 548
Reputation: 133560
It is better you download and display images when you need instead of having so many images in drawable folder.
You should display btimaps efficiently. Load a scale down version in memory
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Also check Managing bitmaps memory in the below link
http://developer.android.com/training/displaying-bitmaps/manage-memory.html.
If you run into memory leaks use a Mat Analyzer to find and fix the same
https://www.youtube.com/watch?v=_CruQY55HOk
If your displaying images from url consider using lazy loading technique
Upvotes: 0
Reputation: 26034
you should not cache so much images in your memory. There is a good library made by nostra.
Library : https://github.com/nostra13/Android-Universal-Image-Loader
Try to implement library. This provides memory as well hard caching which will not let your application force close due to memory.
Upvotes: 2