0
votes

I have a RecylerView in which each item contains Iamge, to load image I'm using Glide. The problem I'm facing is that some images in the list not showing up and in my onException call back both Exception and model are null for these images.

enter image description here

if you will see the fourth item in the list images is not showing.

Here is how i am coding this

Glide.with(context).load(productItem.getImg())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        if( e != null) {
                            e.printStackTrace();
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                }).
                into(holder.imageViewModel);
enter code here

and in xml i have ImageView declare like this

 <ImageView
        android:id="@+id/imageViewModel"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="@dimen/cart_vertical_margin"/>
1
Use SimpleTarget to load your bitmap in which you can give your desire height and width for load image from server. - Piyush
SimpleTarget ? how can i use SimpleTarget ? - user2934930
some time glide throws exception in case it won't able to load or some issue like network etc. I have explain best way to use glide , believe it will help you a lot. please refer this link stackoverflow.com/a/39336438/3636561 - Abdul Rizwan

1 Answers

0
votes

Use following:

Glide.with(mContext).load(productItem.getImg()).asBitmap().centerCrop().into(new SimpleTarget<Bitmap>(200, 200) {
            @Override
            public void onResourceReady(Bitmap arg0, GlideAnimation<? super Bitmap> arg1) {
                // TODO Auto-generated method stub
                if (arg0 != null) {
                    holder.imageViewModel.setImageBitmap(arg0);
                } else {
                    holder.imageViewModel.setImageResource(R.drawable.ic_launcher);
                }
            }
        });