I’ve created a photo gallery that allows you to select multiple photos, but I’ve noticed that populating the GridView with thumbnails can be very slow. I use a timer:
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
fetchNextThumb();
}
}, 0, 25);
It searches the GridView for children without thumbnails (which I keep disabled), retrieves the proper thumb from MediaStore if it’s not already cached, and performs setImageBitmap() on the UI thread:
private void fetchNextThumb()
{
for (int i = 0; i < gridView.getChildCount(); i++)
{
ImageSelectView isv = (ImageSelectView)gridView.getChildAt(i);
if (isv == null)
continue;
if (isv.isEnabled())
continue;
activeThumb = thumbCache.get(isv.getId());
if (activeThumb == null)
{
activeThumb = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), isv.getId(), MediaStore.Images.Thumbnails.MICRO_KIND, null);
thumbCache.put(isv.getId(), activeThumb);
}
activeView = isv;
runOnUiThread(setNextThumb);
return;
}
}
private Bitmap activeThumb;
private ImageSelectView activeView;
private Runnable setNextThumb = new Runnable() {
public void run() {
activeView.setImageBitmap(activeThumb);
activeView.invalidate();
activeView.setEnabled(true);
}
};
It’s incredibly quick at first, but starts slowing down after about 50 or so thumbnails have been grabbed. Anyone know any tricks to speeding up thumbnail retrieval?
Well, I figured out why some thumbnails are loading faster than others. I’m using
which apparently grabs images from both the SD card and the device. Unsurprisingly, the thumbs for SD card images load way slower than those on the device. Still though, the built-in gallery doesn’t seem to have this problem. My solution for now will be to save on-device thumbnails for SD card images after loading them once.
For anyone interested in implementing a custom photo gallery, the code above should work pretty well. Feel free to message me with any questions.