Picasso image caching

后端 未结 1 1283
情书的邮戳
情书的邮戳 2021-01-19 03:57

I want to download the following image downloading code with Picasso image cache.

DownloadImage downloadImage = new DownloadImage(); 
downloadImage.execute(a         


        
1条回答
  •  盖世英雄少女心
    2021-01-19 04:45

    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.

    0 讨论(0)
提交回复
热议问题