Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso image caching

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

DownloadImage downloadImage = new DownloadImage(); 
downloadImage.execute(advert.getImgUrl());

private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... arg) {
        Bitmap bmp = null;
        try {
            URL url = new URL(arg[0]);
            bmp = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return bmp;

    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result == null) {
            Intent intent = new Intent(AdvertisingActivity.this,
                    AdvertisingErrorActivity.class);
            intent.putExtra("ad-error", "Error downloading image");
        }
        adImg.setImageBitmap(result);
        super.onPostExecute(result);
    }
}

I have several questions regarding this.

  1. I want to download more than one image in parallel. If I make repeated calls of Picasso.with(getActivity()).load(url); with different url values, does this get done?

  2. I want to download images in one activity and use it in another activity. Is this possible? How can this be done?

  3. If I call Picasso.with(getActivity()).load(url); more than once with the same url value, does this load the cached images for subsequent calls after the image has been downloaded?

  4. If the image download process does not succeed for some reasons, can you make Picasso report you of the failure?

like image 429
Sandah Aung Avatar asked Mar 07 '14 13:03

Sandah Aung


People also ask

What is Picasso library in Android?

Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application.

What is Picasso API?

A powerful image downloading and caching library for Android.

What is Fresco cache?

Fresco is a graphics library for displaying and managing images in Android applications. It caches the image in a memory-efficient manner. One of the most important features is that it displays a placeholder image until the image loads from the URL. This saves on data and makes efficient use of the CPU.


1 Answers

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.

like image 110
Ellis Avatar answered Sep 27 '22 21:09

Ellis