-1

Care: No code here, only text and some questions about bitmap caching

I'm currently developing an App which is almost finished. The only thing left, that I would like to do is caching images. Because, at the moment, when the user opens the app the app downloads images from a server. Those images are not static, that means they can change every minute/hour/day. I don't know when they change, because it's a list of images gathered by the amount of twitter shares, facebook likes etc. That means, when a picture has 100 likes and 100 tweets it is place 1. But when another picture gets more likes and tweets it gets rank 1 and the other one will be placed as rank 2. This isn't exactly my app, but just so you understand the principle.

Now I looked into Bitmap caching so the user doesn't have to download the same images over and over. The question I do have is how do I do it? I mean, i Understand HOW to cache bitmaps.

I looked into this documentation article: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

But, the problem is, how do I know if the Bitmap already got downloaded and has been cached or if I have to download it again? Don't I have to download the image first to check if I have this particular image already in my system?

I thought about getting the URL of the image, then convert it into a hash. And then, save the files to the cache with the hash as filename. Then, when the image URL comes it will be checked wether the image is available in the cache or not. If it is it will be loaded if not it will be downloaded. Would that the way to go be?

Or am I misunderstanding bitmap caching and it does it from its own already?

Musterknabe
  • 5,763
  • 14
  • 61
  • 117

3 Answers3

2

my best advice on those cases is: Do not try to re-invent the wheel.

Image loading/caching is a very complex task in Android and a lot of good developers already did that. Just re-use their work.

My personal preference is Picasso http://square.github.io/picasso/

to load stuff with it is one very simple line of code:

Picasso.with(context).load(url).into(imgView);

it's that simple!

It does both RAM and disk cache, handles all threading issues and use the excellent network layer okHttp.

edit:

and to get access directly to the Bitmap you can:

Picasso.with(context).load(url).into(new Target() {
  void onBitmapLoaded(Bitmap bitmap, LoadedFrom from){
      // this will be called on the UI thread after load finishes 
  }

  void onBitmapFailed(Drawable errorDrawable){
  }

  void onPrepareLoad(Drawable placeHolderDrawable){
  }

});

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Thanks for the answer, but here again I have the question if I can also save the bitmap from picasso in a Bitmap variable. Like `Bitmap bitmap = picasso.with(context).load(url)` And afterwards use it next to a textview `Drawable d = new BitmapDrawable(getResources(), bitmap); textView.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);` – Musterknabe Nov 08 '13 at 09:45
  • 1
    yes. It's a really great library. I use it for compound drawables. Check my edit. – Budius Nov 08 '13 at 09:52
  • @JakeWharton explains it here github.com/square/picasso/issues/308 in the last comment. – Etienne Lawlor May 05 '14 at 22:30
1

Check this library: http://code.google.com/p/android-query/wiki/ImageLoading

It does caching automagically

example

//fetch a remote resource in raw bitmap

String url = "http://www.vikispot.com/z/images/vikispot/android-w.png";

aq.ajax(url, Bitmap.class, new AjaxCallback<Bitmap>() {

        @Override
        public void callback(String url, Bitmap object, AjaxStatus status) {

        }
});.

http://code.google.com/p/android-query/wiki/AsyncAPI

Malachiasz
  • 7,126
  • 2
  • 35
  • 49
  • Hi, I already saw that library, but there's a problem. I'm also downloading Favicons and displaying them next to a textview with `setCompoundDrawablesWithInstricBounds` - Is it possible with that library to save the outcome to a Bitmap instead directly displaying it into an imageview? So I can set it next to a TextView? – Musterknabe Nov 08 '13 at 09:17
  • yes you should extend AjaxCallback and later before displaying bmp check if it's not cached with aq.getCachedImage(). Look at my edited answer. – Malachiasz Nov 08 '13 at 09:25
0

You can try https://github.com/thest1/LazyList

the project code was designed for listviews, but still, its purpose is to download images from URLs in the backgroud so the user doesn't have to hold on the whole downloading time.

you take these JAVA classes : FileCache, ImageLoader, MemoryCache, import them into your project,

for downloading an image you just call imageLoader.DisplayImage(URL,ImageView);

the best part is that it takes care of the cache itself so you don't have to worry about that

hope this helps

TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50