Question

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?

Was it helpful?

Solution

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/

OTHER TIPS

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);

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);
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.

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.

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....

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

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

compile 'com.github.bumptech.glide:glide:3.6.1'

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);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top