I’m trying to implement an image gallery in android.
The code based on http://www.mobisoftinfotech.com/blog/android/android-gallery-widget-example-and-tutorial/ and i’ve changed some details.
I’m using WeakReference and it seems, that when i’ve too many bitmaps, the garbage collector destroys my weakreferences. How can i handle this?
I get my bitmaps via this function:
public static WeakReference<Bitmap> getBitmap(String imageName, int width,
int height) {
String pathToImage = getPathToImage(imageName);
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathToImage, options);
/*
* Calculate inSampleSize
*/
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
WeakReference<Bitmap> scaledBitmap = new WeakReference<Bitmap>(
BitmapFactory.decodeFile(pathToImage, options));
return scaledBitmap;
}
And i’ve taken the Solution 320×480, so i think it is not this big…
When the gallery has more than 3 pictures, some of them aren’t displayed.
Is the gallery-tutorial not that good? Are there other ways to implement this?
Thank you!
Instead of using soft references, you should take a look at the lrucache class (it became available in honeycomb, but is part of the android-support library.
You can read more about it here : http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
It’s pretty convenient : use this and you won’t have to handle the memory yourself with weak references 🙂