Picasso Library, Android: Using Error Listener

后端 未结 6 1515
说谎
说谎 2020-11-29 05:11

I\'m playing around with the Picasso library for image loading, but I\'m running into an issue. When an image fails to load, I want to hide the view rather than load in a de

相关标签:
6条回答
  • 2020-11-29 05:38

    Just a suggestion, but you might avoid issues in programming if you make an "empty" png file and set it as the default image file in your res folder... kinda silly i know... but likely to work without fighting...

    0 讨论(0)
  • 2020-11-29 05:46

    When we got error, error goes to onError method then we handle it!

    private void getAvatar(){
            Picasso.with(this)
                    .load(Links.GET_AVATAR + ".jpg")
                    .into(imgUserAvatar, new Callback() {
                        @Override
                        public void onSuccess() {
    
                        }
    
                        @Override
                        public void onError() {
                            imgUserAvatar.setImageResource(R.drawable.icon_profile_default);
                        }
                    });
    }
    
    0 讨论(0)
  • 2020-11-29 05:57

    My answer:

    File file = new File(filePath);
            Picasso.with(context).load(file).placeholder(R.drawable.draw_detailed_view_display).error(R.drawable.draw_detailed_view_display)
            .resize(400, 400).into(mImageView, new Callback() {
    
                @Override
                public void onSuccess() {
    
                }
    
                @Override
                public void onError() {
                    mImageView.setVisibility(View.GONE);
                }
            });
    
    0 讨论(0)
  • 2020-11-29 05:58

    My example:

    Picasso picasso = new Picasso.Builder(parent.getContext())
                .listener(new Picasso.Listener() {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                    //Here your log
                    }
                })
                .build();
        picasso.load(shopModel.getShopImg())
                .fit()
                .into(viewHolder.shopImg);
    
    0 讨论(0)
  • 2020-11-29 06:00

    You can try to add a 'global' listener.

        // create Picasso.Builder object
        Picasso.Builder picassoBuilder = new Picasso.Builder(this);
    
        picassoBuilder.listener(new Picasso.Listener() {
            @Override
            public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                Log.e("PICASSO", uri.toString(), exception);
            }
        });
    
        // Picasso.Builder creates the Picasso object to do the actual requests
        Picasso picasso = picassoBuilder.build();
    
        try {
            Picasso.setSingletonInstance(picasso);
        } catch (IllegalStateException ignored) {
            // Picasso instance was already set
            // cannot set it after Picasso.with(Context) was already in use
        }
    

    Any subsequent calls to Picasso.with(Context context) will return the instance which connected to listener, so all fails will be logged.

    Please note that you need to call setSingletonInstance as soon as possible, e.g. in Application onCreate.

    P.S. Code adopted from here - Customizing Picasso with Picasso.Builder

    0 讨论(0)
  • 2020-11-29 06:03

    Picasso 2.0 allows you to attach a callback into a request.

    https://github.com/square/picasso

    The callback you are using is for "global" listener and it helps you debug errors that potentially happen due to a network load.

    Use load(url).into(view, new Callback() {...}); in Picasso 2.0.

    Remember to invoke cancelRequest(target) if you are using a Callback.

    0 讨论(0)
提交回复
热议问题