Question

I have a big problem with loading images from cache on the getView of my Listadapter. For image-loading we are using Picasso, and there I build a construct for loading images from cache and if there are no available images, THEN the images should be load from an online-URI.

(Here is a method, which is called inside of my getView of my Adapter)

 @Override
protected void loadImages(final VehicleResultListItem inCurrentItem) {

    // Try to load async the images from local path

    Picasso.with(mContext)
           .load(new File(mContext.getExternalFilesDir(null),
                          inCurrentItem.getVehicleId()))
           .placeholder(inCurrentItem.getServiceType().getPlaceholderId())
           .noFade()
           .into(new Target() {
               @Override
               public void onBitmapLoaded(final Bitmap bitmap,
                                          final Picasso.LoadedFrom from) {
                   mThumbnailImgView.setImageBitmap(bitmap);
               }

               @Override
               public void onBitmapFailed(final Drawable errorDrawable) {
                   // If the local-path-loading fails, try to grab the images online.
                   Picasso.with(mContext)
                          .load(inCurrentItem.getImageURIs().get(0).getImageUriSizeM())
                          .placeholder(inCurrentItem.getServiceType().getPlaceholderId())
                          .into(new Target() {
                              @Override
                              public void onBitmapLoaded(final Bitmap bitmap,
                                                         final Picasso.LoadedFrom from) {
                                  // On succesfully online-loading of the images, store and show them
                                  mThumbnailImgView.setImageBitmap(bitmap);
                                  saveToInternalSorage(bitmap, inCurrentItem);
                              }

                              @Override
                              public void onBitmapFailed(final Drawable errorDrawable) {
                                  // NO interaction
                              }

                              @Override
                              public void onPrepareLoad(final Drawable placeHolderDrawable) {
                                  // No interaction
                              }
                          });
               }

               @Override
               public void onPrepareLoad(final Drawable placeHolderDrawable) {
                   mThumbnailImgView.setImageDrawable(placeHolderDrawable);
               }
           });
}

The problem is, that the image is loading correctly, but the imageView is not updating after image is loaded. In the OnImageLoaded, I am not in the UI-Thread, so ofc it does not update the view. So i tried anything:

It looks like the Threads are getting confused or something like that.

Maybe there is a problem in calling Picasso inside of a Picasso-Target?

Do u have any suggestion?

Greetz and thx

Was it helpful?

Solution

Target must not be an anonymous class. Picasso keeps a WeakReference on Target that will most likely get gc'ed by the time the request completes.

Javadoc clearly states that.

Make your View implement Target instead and use that as the target or keep a reference to your target instances.

Also Picasso handles disk caching for you automatically, unless you've shipped the assets with your app, I would recommend you simply download them and then let Picasso manage the disk/memory cache for you.

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