DanMan
DanMan

Reputation: 1063

Android - Glide in the recyclerView

I am new in android developing and i have an issue. I try to load some images in a RecyclerView. The incoming data have the type byte[].

My first solution is converting byte[] to Bitmap and it works fine but the bitmaps stucks in the memory and i was getting an OutOfMemory exception.

The second solution is using Glide.

Glide.with(mContext)                 
                .load(field.getImage())
                .into(holder.mImageView);

But with this solution i get same images in each item. I think the problem is in the Glide's cache because incoming arrays are different. I try to do solve this with this code:

requestOptions.skipMemoryCache(true);
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(mContext)
                .applyDefaultRequestOptions(requestOptions)
                .load(field.getImage())
                .into(holder.mImageView);

 }

But it doesn't help. What is the right way to do this?

Upvotes: 0

Views: 5367

Answers (3)

amit pandya
amit pandya

Reputation: 1388

For resizing image and adding (placeholder and error image) use this code:

    RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.app_icon);
        requestOptions.error(R.drawable.ic_image_error);
        Glide.with(context)
                .setDefaultRequestOptions(requestOptions)
                .asBitmap()
                .load(newsContentModel.getImgNewsUrl())
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
//                        requestLayout()
//                        Call this when something has changed which has
//                        invalidated the layout of this view.
                        myViewHolder.imgImage.requestLayout();
                        myViewHolder.imgImage.getLayoutParams().height = newHeight;
                        myViewHolder.imgImage.getLayoutParams().width = newWidth;

                        // Set the scale type for ImageView image scaling
                        myViewHolder.imgNewsImage.setScaleType(ImageView.ScaleType.FIT_XY);
                        myViewHolder.imgImage.setImageBitmap(bitmap);
                    }
                });

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69724

try this use setDefaultRequestOptions(requestOptions)

Glide.with(context)
     .setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
     .load(url)
     .into(imageView);

Upvotes: 2

saurabh dixit
saurabh dixit

Reputation: 873

you can resize image like this

 Glide.with(PhotoGalleryActivity.this)
                                    .using(new FirebaseImageLoader())
                                    .load(storageReference)
                                    .asBitmap()
                                    .into(new SimpleTarget<Bitmap>(520, 520) {
                                        @Override
                                        public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                                            holder.img.setImageBitmap(resource);

                                        }
                                    });

Upvotes: 0

Related Questions