Why use Android Picasso library to download images?

62,006

Solution 1

Just for the record for anyone new to Android or perhaps moving to Android from iOS ..........

Until something drastically changes, you absolutely have to use Picasso. Not a joke.

Honestly, it's that simple. The advantages are unbelievable.

It's this easy to use:

Picasso.
  with(State.mainContext).
  load(parseImageFile.getUrl()).
  into(null);

You very simply:

must do caching, and threading, with image handling on Android.

It's that simple. Unless you want to write that from scratch, you simply must use Picasso.

Note that ParseImageFile essentially doesn't work - it is utterly useless about caching and so on. There are admirable alternatives to Picasso (such as Universal Image Loader, check it out), but none work as well as Picasso, for now 2014.

Note if you move to super-advanced-stuffs... The only thing better than Picasso, is to make the move to Volley. but that is a huge leap.

Note that ListView scrolling on android is much, much more problematic than table handling scrolling on iOS. You could say, Android + Picasso is MORE LIKE the situation on iOS, where all the work is already done for scrolling large table views with images.

For today, Picasso is - simply - a central part of any Android app whatsoever. Thank goodness, it is one line of code - no setup, nothing.

Again, the only thing "better than" Picasso is if you move to Volley.

BTW here's an excellent long article on Volley v. Picasso, if you need that...

http://www.bignerdranch.com/blog/solving-the-android-image-loading-problem-volley-vs-picasso/

Solution 2

Picasso download the image in another thread and it manages for you:

  • the placeholder in the meantime the image is still downloading
  • resizing
  • cropping/centering/scaling
  • caching ( you don't have to download the image every time)
  • it even does "image fade in", which is popular/normal now

It's extremely simple, here is an example:

    Picasso.with(context)
           .load(url)
           .placeholder(R.drawable.placeholder)
           .resize(imgWidth, imgHeight)
           .centerCrop()
           .into(image);

Solution 3

I always used Picasso Library for images.
It's very useful for managing images and no worry about Memory problem.
When I' download images from server or json , I used

 Picasso.with(context).load("image url").fetch();

And i store that image url to Database or somewhere.
Now we can use that image in anywhere (offline also).

Picasso.with(context).load("image url").into(ImageView);

Solution 4

Picasso.with(this).load("http://webneel.com/wallpaper/sites/default/files/images/04-2013/island-beach-scenery-wallpaper.jpg").placeholder(R.mipmap.ic_launcher).fit().into(imageView,
  new Callback() {@
    Override
    public void onSuccess() {}@
    Override
    public void onError() {}
  });

You should download the images via the Picasso library because of the following reasons:

  1. You can put a placeholder in case the image takes some time to load.
  2. fit() - sometimes some images do not load in imageview because of the size. This method will help you to load large images.
  3. onSuccess() - you can perform some action when an image loads successfully.
  4. onError() - you can perform some action when there is a problem loading an image.

Solution 5

You should use an image loader library like Picasso, Volley or Universal Image Loader because they do the following things that your code doesn't do:

  • Efficient multithreaded networking (on background threads of course)
  • Combining multiple identical requests into a single network call
  • Canceling pending requests, especially during ListView items recycling
  • Disk and memory caching with various expiration policies
  • Images downsampling to the target view size to improve performance and reduce memory usage
  • Batching UI updates to improve UI responsiveness (at least for Volley and Picasso).

By the way, you must never perform network requests on the UI thread and since HoneyComb, Android doesn't let you do it.

Share:
62,006

Related videos on Youtube

user3376321
Author by

user3376321

Updated on March 12, 2020

Comments

  • user3376321
    user3376321 over 4 years

    Why should I download the images via the Picasso library instead of just using this code:

    private Bitmap DownloadImage(String URL) 
    {
        Bitmap bitmap = null; 
        InputStream in = null; 
    
        try 
        {
            in = OpenHttpGETConnection(URL);
            bitmap = BitmapFactory.decodeStream(in); in.close();
        } 
        catch (Exception e) 
        {
            Log.d("DownloadImage", e.getLocalizedMessage());
        }
    
        return bitmap; 
    }
    

    Another question:

    Does Picasso download the image in the UI or by background thread?

    • njzk2
      njzk2 over 10 years
      If you have to ask, don't use it. If you don't see what it can bring to your application, it means you don't need it so far.
    • user3376321
      user3376321 over 10 years
      ok, do you know for which android versions does picasso support?
    • njzk2
      njzk2 over 10 years
      no. but I am sure they have a web site with this kind of information.
  • dnkoutso
    dnkoutso over 10 years
    +1. It will also handle ListView recycling for you. I highly recommend you do not re-invent the wheel. Use any of the existing image download libraries in your app.
  • user3376321
    user3376321 over 10 years
    thank you very much, is there any tutorial that i could start with it ?
  • Sarpe
    Sarpe over 10 years
    picasso is an open source project from square, here is a good starting point: square.github.io/picasso
  • user3376321
    user3376321 over 10 years
    I have a problem, this my code : @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.channel); image=(ImageView)findViewById(R.id.icon2); Picasso.with(this).load("i.imgur.com/DvpvklR.png").into(im‌​age); when i run the app i got an error
  • njzk2
    njzk2 over 10 years
    @user3376321 : I recommend you ask a new question for this one. Include all relevant code, xml layout, as well as the error stacktrace.
  • user3376321
    user3376321 over 10 years
    excuse me ! How much memory does picasso use for caching ?, and does this effect the size of the app?
  • Fattie
    Fattie about 10 years
    Hi user -- (1) you should click to change to a nickname. click edit on your profile. (2) Picasso adds NOTHING to the size of your app (tiny amount). (3) it only uses the memory "it is able to". it is completely transparent, and you don't have to worry about it. basically every app with images uses Picasso, so, you can see they all work :) enjoy
  • android developer
    android developer almost 10 years
    Since listView has a getView which recycles views, how would you cancel the downloading of the image for the imageView that should now show a different image-url ? Also, what will happen when you leave the activity? wouldn't you need to tell it to cancel all pending operations of it?
  • Ahmed Hegazy
    Ahmed Hegazy almost 10 years
    For clarity Volley is not an image caching Library, you can use Glide github.com/bumptech/glide that provides image caching using Volley.
  • Fattie
    Fattie almost 10 years
    @Hegazy - brilliant tip, I did not know about Glide! Right, I meant writing one's own, using Volley. Thanks again.
  • aagam94
    aagam94 over 9 years
    But Picasso is just for caching! If i want to use it offline then the cache may not be present and finally it will show a placeholder every time. Any solution for that @JoeBlow
  • Fattie
    Fattie over 9 years
    hi aaagam, it is a totally unrelated issue with no connection to this question, I urge you to ask a new question about that - it's a good question!
  • nikhil
    nikhil over 8 years
  • afruzan
    afruzan over 8 years
    same as my solution. plus one for you :)
  • crubio
    crubio almost 7 years
    Picasso is not working with SSL. I've tried to modify it to trust all certs (I know it's unsafe stackoverflow.com/questions/23562794/…) but it gives me random crashes because I need to use older versions of okhttp. I'm considering using an alternative. I have lost too much time with this issue.