First time error loading picture with Picasso

最后都变了- 提交于 2019-11-27 19:04:47

问题


I have an Activity with an ImagePagerAdapter (extends of FragmentStatePagerAdapter) that has this getItem method:

@Override
        public Fragment getItem(int position) {
            Log.d(LOGTAG, "------------>mUserPicturesList.get("+position+").getFilename(): " + mUserPicturesList.get(position).getFilename());
            return UserDetailFragment.newInstance(mUserPicturesList.get(position).getFilename());
        }

The fragment that is instantiated has this onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate and locate the main ImageView
    final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
    mImageView = (ImageView) v.findViewById(R.id.imageView);
    mProgressPicturePager = (ProgressBar) v.findViewById(R.id.progress_picture_pager);

    String imageUrl = WApp.PHOTO_URL + mImageUrl + "?type=user_gallery_big_img";

    Picasso picasso = Picasso.with(getActivity());
    picasso.setDebugging(true);
    picasso.load(imageUrl)
            .placeholder(R.drawable.no_picture_man_big)
            .error(android.R.drawable.stat_notify_error)
            .into(mImageView, new Callback() {
                @Override
                public void onSuccess() {
                    mProgressPicturePager.setVisibility(View.GONE);
                }

                @Override
                public void onError() {
                    Log.d(LOGTAG, "picasso load error");
                    mProgressPicturePager.setVisibility(View.GONE);
                }
            });

    return v;
}

The Problem:

When the ImagePager load first time, some times, Picasso call onError, showing the .error drawable. If I press on back button and go back to the Activity that has the ImagePager, Picasso load the picture correctly. If the ImagePager has two or more pictures and I swipe between the pictures, those are loaded correctly some times without exit and reenter to the ImagePager.

The Theories:

I think that it could be a problem of cache, but after many searches, I bet that the problem is in the weak reference of the Picasso. Keep in mind that the problem only appears the FIRST TIME I load the activity that have the ImagePager.

In another place, currently Picasso works fine in listView with an adapter loading the pictures at first time. Calling Picasso inside the getView method of Adapter class.

Visited links

  • How would an anonymous class get GC'd in picasso on Android?
  • ViewPager unable to load images lazily with Picasso
  • http://square.github.io/picasso/
  • https://plus.google.com/communities/109244258569782858265/stream/885843f4-c8b5-4851-9de1-b0374121dfa3
  • Use of Target in Picasso on Adapter
  • https://github.com/square/picasso/pull/349

Thanks in advance.


回答1:


The problem was solved in the Picasso 2.3.0.

The fix is in the Picasso changelog:

Requests will now be automatically replayed if they failed due to network errors.

I hope this save you many hours.



来源:https://stackoverflow.com/questions/21890598/first-time-error-loading-picture-with-picasso

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