Reputation: 1048
I am using Volley to download an image, save it to a diskcache and show it on an ImageView. I have done it succesfully, but what I want now is to resize the Bitmap downloaded to a new dimension (kind of a thumbnail). It will be great if I could save to disk both bitmaps, the original and the thumbnail.
Can you give me some hint about how can I do it?
Upvotes: 0
Views: 666
Reputation: 8690
Managing Bitmaps in Android is a complicated and memory consuming matter. The most efficient way to handle thumbnails is by creating a thumbnail version for every image you hold on the server, that way the image manipulation has already been done "off-line" and all you need to do is use the correct version. You can effectively download a bunch of small images must faster, and lazily load the full size image upon request.
However, if you do not control the server that holds the images, and don't care to create such a server yourself, here's one way you can accomplish what you want:
Create a simple Bitmap
cache for memory caching (or disk caching, or both you'd like).
When you want to display an image in your ImageView
(the one which displays the re-sized images) do the following:
a. Check the cache if the thumbnail exists - if it does return and display it
b. If it doesn't request it and assign a listener for the result.
Every image which needs a thumbnail version should be requested using the ImageLoader.get()
method with a custom ImageListener
:
public class MyImageListener implements ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
// handle errors
}
@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
// manipulations, resize, etc.
// save manipulated bitmap in cache
// notify listener from section 2.b. if one exists
}
}
}
Upvotes: 2