Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
178 views
Welcome To Ask or Share your Answers For Others

1 Answer

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){
  }

});


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...