is there a way to load a default image in picasso

sapamlucy picture sapamlucy · Oct 31, 2014 · Viewed 15.5k times · Source

i cannot load an image in picasso from my drawable due to resons known only to picasso, so whever picasso fails to load i want to load a default image please help

    @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                 final Context context =getApplicationContext();
                 final ImageView imageView = new ImageView(getApplicationContext());
              int padding = context.getResources().getDimensionPixelSize(
                  R.dimen.padding_medium);
              imageView.setPadding(padding, padding, padding, padding);

             PicassoTools.clearCache(Picasso.with(context));
             ((ViewPager) container).addView(imageView, 0);
                      imageView.setTag("myview" + position);

            Picasso.with(context).load(mImages[position]).resize(320,280).centerInside().placeholder(placeholderDrawable)
                .into(imageView,new Callback() {

                    @Override
                    public void onError() {
                        // TODO Auto-generated method stub
                        imageView.setImageResource(R.drawable.c3);
                    }

                    @Override
                    public void onSuccess() {
                        // TODO Auto-generated method stub

                    }

                        });

iv included callback in the hope to do something but my brain is not working, any1 help

Answer

Moonbloom picture Moonbloom · Oct 31, 2014
Picasso.with(context).load(www.google.com/image/1).placeholder(context.getResources().getDrawable(R.drawable.default_person_image)).error(context.getResources().getDrawable(R.drawable.default_person_image)).into(pictureView);

This is what i'm currently using (placeholder URL of course). It will try and load the image you provide in the "load()" part, will show the "placeholder()" part before it has downloaded the image, and if it fails it will show the "error()" part.

Personally i have both the placeholder() and error() part to show the same image, but you can load two different images.