Picasso - Image downloading and caching library for Android

useful android libraries
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Picasso - Image downloading and caching library for Android

Postby vivekkalady » Sat Nov 22, 2014 9:19 am

Picasso allows for hassle-free image loading in your application—often in one line of code!

Prerequisites:

  • Create new Android project using your favorite IDE (I’ll using Eclipse).
  • Download Android Picasso library.
  • Put the downloaded jar file into your project’s “libs” folder.

Simple Usage

Code: Select all

Picasso.with(context).load("http://forum.androidappmasters.com/styles/prosilver/theme/images/site_logo.png").into(imageView);


context is the application context.if you are in an fragment, you can use getActivity()
imageView is the object of imageview when image is to be placed

Place Holders

Picasso supports both download and error placeholders as optional features.

Code: Select all

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);


Resource Loading

Resources, assets, files, content providers are all supported as image sources.

Code: Select all

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load(new File(...)).into(imageView2);



Original Source : http://square.github.io/picasso/


Save Loaded image to SDCard using Target Class


Target class provided with the Picasso library to convert our image to bitmap. It actually has 3 three methods but we are just concerned with onBitmapLoaded as we only need to save the image to disk when it is loaded.

Code: Select all

Picasso.with(this)
.load("http://forum.androidappmasters.com/styles/prosilver/theme/images/site_logo.png")
.into(target);


Code: Select all

private 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() + "/image1.jpg");
            try {
               file.createNewFile();
               FileOutputStream ostream = new FileOutputStream(file);
               bitmap.compress(CompressFormat.JPEG, 80, ostream);
               ostream.close();
            } catch (Exception e) {
               e.printStackTrace();
            }

         }
      }).start();
   }
   
   @Override
   public void onBitmapFailed(Drawable errorDrawable) {}

   @Override
   public void onPrepareLoad(Drawable placeHolderDrawable) {
      if (placeHolderDrawable != null) {}
   }
};

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 1 guest

cron