Hey guys,
I'm using a lazy image loader for my ListView with the following code inside getView()
:
Bitmap cachedImage = asyncImageLoader.loadBitmap(item.getImage(), wallpaperNumber, new ImageCallback()
{
public void imageLoaded(Bitmap bitmapImage, String wallpaperNumber)
{
ImageView imageViewByTag = (ImageView) listView.findViewWithTag(wallpaperNumber);
imageViewByTag.setImageBitmap(bitmapImage);
}
});
And the following inside the AsyncImageLoader
class:
public Bitmap loadBitmap(final byte[] image, final String wallpaperNumber, final ImageCallback imageCallback)
{
if (imageCache.containsKey(wallpaperNumber))
{
SoftReference<Bitmap> softReference = imageCache.get(wallpaperNumber);
Bitmap Bitmap = softReference.get();
if (Bitmap != null)
{
return Bitmap;
}
}
final Handler handler = new Handler()
{
@Override
public void handleMessage(Message message)
{
try
{
if (message.obj != null)
{
imageCallback.imageLoaded((Bitmap) message.obj, wallpaperNumber);
}
else
{
Bitmap bitmapImage = loadImage(image);
imageCache.put(wallpaperNumber, new SoftReference<Bitmap>(bitmapImage));
imageCallback.imageLoaded(bitmapImage, wallpaperNumber);
}
}
catch (Exception e)
{
MiscFunctions.logStackTrace(e);
}
}
};
new Thread()
{
@Override
public void run()
{
Bitmap bitmapImage = loadImage(image);
imageCache.put(wallpaperNumber, new SoftReference<Bitmap>(bitmapImage));
Message message = handler.obtainMessage(0, bitmapImage);
handler.sendMessage(message);
}
}.start();
return null;
}
private static Bitmap loadImage(byte[] image)
{
Bitmap bitmapImage = BitmapFactory.decodeByteArray(image, 0, image.length);
bitmapImage = Bitmap.createScaledBitmap(bitmapImage, imageWidth, imageHeight, true);
return bitmapImage;
}
public interface ImageCallback
{
public void imageLoaded(Bitmap imageBitmap, String wallpaperNumber);
}
For an unknown reason to me, after to switched from using the common SQLiteOpenHelper
to a custom one which allows me to store my database on the SD card, every 6th or 7th image that is loaded throws a NullPointer at:
imageCallback.imageLoaded((Bitmap) message.obj, wallpaperNumber);
I've checked everything and don't actually know what variable is actually null.
Any help?