Mr.Sha1
Mr.Sha1

Reputation: 530

Getting large number of images in recyclerView android

in my recent android project I have to get a large number of images from the web and show them in my main activity (something like explore part of the Instagram app). I used a recyclerView with gridLayoutManager of 3 columns and I have to get something like 700 images from URL. When I open the app and comment the getting image part, app work perfectly fine (the app get the information of each tile but doesn't display the images). But when I start setting image bitmaps the app start becoming laggy and crash after about a hundred images loaded. What do you recommend me to do ? And another question: Was using recyclerView a smart idea ? Thanks for your reply.

Upvotes: 0

Views: 1304

Answers (2)

vikas kumar
vikas kumar

Reputation: 11018

You should post some code here but seems you are asking for some efficient setup.

First of all, try using some image caching libraries like Glide or Picasso. It manages and caches your images locally so you don't end up making multiple requests for the same image.

This solves most of your problem and don't try to load 700 images altogether and display use lazy loading means load first 10-20 images first and when user scrolls make another API call for another 10-20 and so on.

Here is an article on how to use Glide and how it works.

https://futurestud.io/tutorials/glide-image-resizing-scaling

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93561

Don't load hundreds of images. That requires a few things:

1)Don't store images in memory in the adapter. Store the url/resourceid/whatever of the image so you can load it on demand.

2)Use an LRU cache of images, limited in size and load your images through that.

3)Make sure that however you download images does not spawn too many concurrent requests, and that requests are canceled when no longer needed (when the view it would go into is recycled).

4)I'd suggest downloading the images and writing them to disk, then loading them from disk as needed. This will prevent you from having to keep the entire file in memory to decode it while downloading it, which will reduce your memory usage while downloading.

5)Do not decode the image on the UI thread. Do it on another thread.

6)If you don't need to display images fullsize, make thumbnails.

Images in a RecyclerView, especially if being downloaded need a lot of work to do well and handle rapid scrolling.

Upvotes: 3

Related Questions