0

I have a recycleView with 50 block. The block has two ImageView. I should set two images to every block from different URL. And when I start my program I flipping the list, my program slows down. I think it inhibits becouse of when I scroll the list recyclerView go to URL again to take images. Am I right? And how I can fix it?

It is how I set image to recyclerView from URL

URL newurl = new URL(imageString);
Bitmap mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
holder.myImageView.setImageBitmap(mIcon_val);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sergey
  • 43
  • 7
  • Possible duplicate of [Lazy load of images in ListView](https://stackoverflow.com/questions/541966/lazy-load-of-images-in-listview) – Manohar Jun 24 '19 at 09:15
  • My Recommendation use [Glide](https://github.com/bumptech/glide) library to load images as answer below told – Manohar Jun 24 '19 at 09:16
  • You can use Picasso library also – Navin Kumar Jun 24 '19 at 09:20
  • Stick with Glide V3 if you want simplicity, V4 if you want latest support + extendability and avoid Picasso since it's very opinionated. You have to adjust your code to their rules in order to make your application work. – Zun Jun 24 '19 at 10:03

3 Answers3

1

You can use picasso or glide for this task-:

Glide.with(context).load(model.getVoucher_image()).into(holder.imageview);
Shivam Oberoi
  • 1,447
  • 1
  • 9
  • 18
0

You are using bitmap to load images. Loading bitmap in image view is very memory consuming task.

Instead you can use third party library to load image from network URL directly.

One of these library is Glide. You just need to pass imageView and URL into it and it will load image directly.

Glide.with(context)
   .load("URL HERE")
   .into(imageView);

Even you can resize image before loading it into imageView, by doing that way, you can reduce memory consumption.

 RequestOptions requestOptions = new RequestOptions();
 requestOptions.override(120, 120);
 requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);

 Glide.with(context)
   .load("URL HERE")
   .apply(requestOptions)
   .into(imageView);
DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
0

Use Glide to load images efficiently.

Example In app module gradle add this

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

then usage for glide to load images from drawable

Glide.with(this).load(R.drawable.image_name).into(imageView);

Example

onBindViewHolder do this

Glide.with(this).load(logoImage).into(holder.logo);
Navin Kumar
  • 3,393
  • 3
  • 21
  • 46