Use Bitmap Caching the correct way

2019-09-21 01:48发布

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?

3条回答
【Aperson】
2楼-- · 2019-09-21 02:19

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

查看更多
乱世女痞
3楼-- · 2019-09-21 02:22

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

});

查看更多
何必那么认真
4楼-- · 2019-09-21 02:29

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

查看更多
登录 后发表回答