Sometimes Picasso does not load an image from the memory cache

 private class CustomAdapter extends CursorAdapter {

@Override
public void bindView(View view, Context context, Cursor cursor) {
    if (view != null) {

        String url = cursor.getString(CONTENT_URL_COLUMN);
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        final ImageView imageView = viewHolder.mImageViewIcon;
        final TextView textView = viewHolder.mTextViewName;

            Picasso.with(context).load(url).into(new Target() {

                @Override
                public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
                    imageView.setImageBitmap(arg0);
                    imageView.setVisibility(View.VISIBLE);
                    textView.setVisibility(View.GONE);
                }

                @Override
                public void onBitmapFailed(Drawable arg0) {
                    imageView.setVisibility(View.GONE);
                    textView.setVisibility(View.VISIBLE);
                }
             });
        }
    }
}
}

If the list of images is already loaded, then when you quickly scroll through the list, the onBitmapLoaded () method is called and loads the image from the memory cache. But sometimes it calls onBitmapFailed () . Why?

+1
source share
1 answer

Your Targetgarbage collects because nothing holds a link to it. Picasso uses WeakReferencewhile holding ImageViewor Targets.

However, you do not need to use Targetat all. Just use the callback .intoand pass ImageViewdirectly.

Picasso.with(context).load(url).into(imageView, new Callback() {
  @Override public void onSuccess() {
    imageView.setVisibility(VISIBLE);
    textView.setVisibility(GONE);
  }

  @Override public void onError() {
    imageView.setVisibility(GONE);
    textView.setVisibility(VISIBLE);
  }
});
+7
source

All Articles