Why Use Android Picasso Library to Download Images

Why use Android Picasso library to download images?

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/

Downloading multiples images from Picasso in Android

Try this way,hope this will help you to solve your problem.

static int  count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

for (int i = 0; i < url.length; i++)
{

// url is String array which has 2 urls.
Picasso.with(this).load(url[i])
.into(new Target() {

@Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub

}

@Override
public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
// TODO Auto-generated method stub
arg0 = Bitmap.createScaledBitmap(arg0, 150, 150, true);
++count; // Incrementing the count by 1
filePath = saveFile(arg0); // I'm just calling this function to check how many times `onBitmapLoaded` is called. And it is called only once...!!
}

@Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub

}
});
}
}

public String saveFile (Bitmap bm)
{

Toast.makeText(getApplicationContext(), ""+count, 100).show(); // Displaying the value of count, which always display as **2**.
return "";
}

How Picasso downloading images

You can browse source-code of Picasso at: https://github.com/square/picasso.

Downloading images

You can see that Picasso downloads images with implementation of Downloader interface. It uses default downloader named OkHttpDownloader, which utilizes OkHttp library. When it can't be loaded, Picasso uses UrlConnectionDownloader.

Recognizing size of the images

Picasso doesn't know size of the images before download. If you are developing back-end server, you can specify size of the images in some way, so your mobile application will know it by performing a concrete request, but it can't be recognized by Picasso itself. Picasso has to download image in a full size and then this image can be cropped or resized by this library.

Cache

We can find the following information about Cache in the Picasso documentation placed in source code:

Picasso instance is automatically initialized with defaults that are
suitable to most implementations.

  • LRU memory cache of 15% the available application RAM
  • Disk cache of 2% storage space up to 50MB but no less than 5MB. (Note: this is only available on API 14+ or if you are using
    a standalone library that provides a disk cache on all API levels like
    OkHttp)
  • Three download threads for disk and network access.

It explains usage of cache in this library quite clearly.
I'm just not sure, if Picasso stores images before transformation (resizing, cropping, etc.) or after transformation in cache. First option seems more reasonable for me, because we decide to apply a different transformation later, so we may want to keep original image.

Saving image from url using Picasso?

Solved. now works fine!

I did

//save image
public static void imageDownload(Context ctx, String url){
Picasso.with(ctx)
.load("http://blog.concretesolutions.com.br/wp-content/uploads/2015/04/Android1.png")
.into(getTarget(url));
}

//target to save
private static Target getTarget(final String url){
Target target = new Target(){

@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
new Thread(new Runnable() {

@Override
public void run() {

File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
Log.e("IOException", e.getLocalizedMessage());
}
}
}).start();

}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};
return target;
}


Related Topics



Leave a reply



Submit