Sometimes Picasso doesn't load the image from memory cache

匿名 (未验证) 提交于 2019-12-03 02:54:01

问题:

 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 ?

回答1:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!