Shanki Bansal
Shanki Bansal

Reputation: 1750

Sometimes Picasso doesn't load the image from 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 list of images get already downloaded, then on fastly scrolling the list, onBitmapLoaded() method called and load the image from memory cache. But sometimes onBitmapFailed() called. Why ?

Upvotes: 1

Views: 3862

Answers (1)

Jake Wharton
Jake Wharton

Reputation: 76125

Your Target is being garbage collected because nothing is holding a reference to it. Picasso uses a WeakReference when holding ImageViews or Targets.

However, you need not use Target at all. Simply use the callback of .into and pass the ImageView directly.

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);
  }
});

Upvotes: 7

Related Questions