Tagged: android-picasso

Android: Offine image caching with Picasso

If you want to cache images for offline access, it’s fairly easy, but you have to include an old version of OkHTTP. Here’s my gradle imports:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.4.0'

After you’ve got those, then in the builder, you need to specify an argument for downloader and give it the OkHttpDownloader class, and give it a max cache size.

  new Picasso.Builder(toolbar.getContext())
    .downloader(new OkHttpDownloader(toolbar.getContext(), Integer.MAX_VALUE))
android android-picasso

Android: Loading an NavigationIcon image into Toolbar via Picasso

Let’s first include the Picasso image loading library in our app’s gradle file:

compile 'com.squareup.picasso:picasso:2.5.2'

And in our beautiful AndroidManifest.xml let us not forget the all important permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Let’s now use Picasso to load our image into a mysterious Target:


What’s the target? Well, since we’re not loading directly into a ImageView, we need this object. It can’t, apparently, be inline either, lest Picasso may garbage collect it. Here it is:

Target target = new Target() {
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    Log.d("HIYA", "onBitmapLoaded");
    Bitmap b = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
    BitmapDrawable icon = new BitmapDrawable(toolbar.getResources(), b);

  public void onBitmapFailed(Drawable errorDrawable) {
    Log.d("HIYA", "onBitmapFailed");

  public void onPrepareLoad(Drawable placeHolderDrawable) {
    Log.d("HIYA", "onPrepareLoad");

We get a reference to the toolbar, and set the navigation icon, after first resizing the image.

Actually, this may not be enough to ensure this isn’t garbage collection. You could make it a field. Ensure you stop the Picasso loader it when your activity/fragment/dog dies.

If you want to go wild, crazy, put it in a BindingAdapter:

public static void setLoadingimage(final Toolbar toolbar, String s) {

And then in your XML, with databinding, set it like this:


The fun thing about this is you may need to make the Target a static variable, else it may be garbage collected.

android android-databinding android-toolbar android-picasso

Page 1 of 1