43

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?

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
user3376321
  • 849
  • 2
  • 9
  • 21

9 Answers9

100

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/

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 8
    For clarity Volley is not an image caching Library, you can use Glide https://github.com/bumptech/glide that provides image caching using Volley. – Ahmed Hegazy Sep 18 '14 at 14:16
  • @Hegazy - brilliant tip, I did not know about Glide! Right, I meant writing one's own, using Volley. Thanks again. – Fattie Sep 18 '14 at 14:24
  • 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 – aagam94 Feb 10 '15 at 17:49
  • 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! – Fattie Feb 11 '15 at 18:51
  • Picasso is not working with SSL. I've tried to modify it to trust all certs (I know it's unsafe https://stackoverflow.com/questions/23562794/doesnt-picasso-support-to-download-images-which-uses-https-protocol/26398617#26398617) 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. – crubio Jul 31 '17 at 10:27
36

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);
Chris
  • 4,593
  • 1
  • 33
  • 37
Sarpe
  • 5,747
  • 2
  • 28
  • 26
  • 8
    +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. – dnkoutso Mar 11 '14 at 16:46
  • thank you very much, is there any tutorial that i could start with it ? – user3376321 Mar 11 '14 at 16:56
  • picasso is an open source project from square, here is a good starting point: http://square.github.io/picasso/ – Sarpe Mar 11 '14 at 17:00
  • 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("http://i.imgur.com/DvpvklR.png").into(image); when i run the app i got an error – user3376321 Mar 11 '14 at 17:16
  • @user3376321 : I recommend you ask a new question for this one. Include all relevant code, xml layout, as well as the error stacktrace. – njzk2 Mar 11 '14 at 17:55
  • excuse me ! How much memory does picasso use for caching ?, and does this effect the size of the app? – user3376321 Mar 11 '14 at 21:24
  • 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 – Fattie Jun 06 '14 at 12:42
  • 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? – android developer Aug 21 '14 at 07:41
12

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);
Kglay Kophyo
  • 121
  • 1
  • 3
5
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.
Jamal
  • 763
  • 7
  • 22
  • 32
Rajat
  • 763
  • 11
  • 17
2

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.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
2

I know that Picasso is an awesome library for managing images in ListView and GridView, But among all options including Picasso, I use GLIDE...

It really manages downloading & Caching Perfectly...(i couldn't get Picasso to use cached images after download completion But Glide did it just like a piece of cake).

Here is the Github page of GLIDE :

https://github.com/bumptech/glide

Regards....

A S
  • 71
  • 6
2

If you will use the core method of loading image from network then it would take larger amount of code. But if we will use a 3rd party library like picasso then we will achieve our goal in few lines of code.
And Picasso Library is the best and simplest I found so far. We only need to write the following to load an image from internet using picasso.

Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.placeholder(Your Drawable Resource) //this is optional the image to display while the url image is downloading
.error(Your Drawable Resource)         //this is also optional if some error has occurred in downloading the image this image would be displayed
.into(imageView);

But if we will not use picasso library or any other library we may need to do it using AsyncTask that will require more lines of code.

Source: Picasso Android Tutorial

Amrah Anam
  • 58
  • 1
  • 6
0

Please add the following dependency in your build.gradle(Module:app)

compile 'com.github.bumptech.glide:glide:3.6.1'
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
nikhil
  • 27
  • very usefull >http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en – nikhil Jan 16 '16 at 10:17
0

Picasso automatically handles all pitfalls associated with image downloading such as caching downloaded image, look up cache, cancelling existing request, performing image transormations and decoding, resizing and cropping of images.

And it allows you to customize it, you can configure memory for cache, add request transformer used for adding global information to every request, add bitmap config to handle decoding images for format not covered by default bitmap config.

It allows you to specify memory policy and network policy which makes it to use either cache or to download from the network. It allows you to add placeholder and error images to be used while downloading and in case of error in downloading respectively.

Picasso downloads images asynchronously on worker threads.

Here is the code using latest version 2.71828 of Picasso to load an image to ImageView on worker thread.

Picasso.get().load(productImageUrl).into(imageView);

Below code resizes and crops image.

See http://www.zoftino.com/android-picasso-image-downloading-and-caching-library-tutorial for information. Picasso.get().load(productImageUrl).resize(400,400).centerCrop().into(imageView);

Arnav Rao
  • 6,692
  • 2
  • 34
  • 31