Picasso image caching

≡放荡痞女 提交于 2019-12-01 18:43:31

I've researched some more into your questions and decided that I should publish this as an answer rather than a comment.

  1. Yes - Picasso loads images asynchronously so making repeated calls will cause images to be downloaded in parallel.
  2. Yes - just make the call as normal and Picasso will handle the re-use of downloaded images e.g. in Activity1, call Picasso.with(this).load("image1"); and, later, make a call to the same URL in Activity2. The image will already be cached (either in memory or on device storage) and Picasso will re-use it, rather than downloading it again.
  3. Yes - see above (Picasso will automatically use cached images where available)
  4. This does not seem to have such a clear-cut answer. One thing you can do is provide an image to display if an error occurs while fetching the real image:

    Picasso.with(context) .load(url) .placeholder(R.drawable.user_placeholder) .error(R.drawable.user_placeholder_error) .into(imageView);

    The 'placeholder' will be displayed whilst the attempt is being made to fetch the image from the web; the 'error' image will be displayed, for instance, if the URL is not valid or if there is no Internet connection.

    Update, 17/03/2014:

    Picasso supports the use of a callback to report you of a failure. Modify your usual call (e.g. the above example) like so:

    .into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            // TODO Auto-generated method stub    
        }
    
        @Override
        public void onError() {
            // TODO Auto-generated method stub
        }
    });
    

In conclusion, it sounds like Picasso would be a great choice of library for you. It definitely makes image downloading very quick and very easy, so I like it a lot.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!