I have this GridView in android in which I display some images that should be downloaded on-demand. The problem is taht the program waits upon every image request to be downloaded completely and then moves on to the next image thus making the loading very slow. I just want to know how should I move the image downloading to the background and display the image when it's ready.
I think I should use AsyncTask for this purpose but I have no idea how should I do this?
Related
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.
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.
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
I am building an Android app that shows full screen images in a carousel. The app downloads 100 objects from the server and stores them in the app. Each object is represented by a full-screen image and some text.
The user will see all downloaded objects as full screen images.
Question: is it more efficient to use lazy loading one image at the time when the user goes through the carousel or download images in full batches?
Google suggests using batching and pre-fetching:
http://developer.android.com/training/efficient-downloads/efficient-network-access.html#BatchTransfers
Also, it is my understanding that one of the key benefits of lazy loading is avoiding downloading images that will not be used. In my case, the user will see all images of the all downloaded objects.
Thanks!
The first target is that the user sees the images without delay. By creating a batch starting from the first image it is probable that he will have the following images loaded when he scrolls through them. If in your case the user can scroll fast through the images, you can change the batch list and set a higher priority on the images he is going to see (like 3 plus and minus from the current position).
In my Android application I have to fetch some images from the server and show them.Same image is shown in some of the pages.Now I am downloading the same image in each page and getting out of memory error.How can I reuse the one that I have downloaded earlier in each page
Use Android Query, and then Image Loading in particular. It's really easy and performs well.
I am trying to load images from the server and want to know how can i convert the images into the thumbnails so that they get uploaded fast and how can i get the larger images from the thumbnails fast . I have tried many things.
Thanks in advance
At the server you need to keep 2 copies of an image. First is the thumbnail and the other one is the image.
First you need to download the thumbnail asynchronously. And if the user wishes to see it then download the original image asynchronously. While downloading original image you can show the scaled thumbnail image as the placeholder image. So when the downloading is completed show the downloaded image.
Make sure to download the images asynchronously otherwise you will block the main thread and UI will get stuck.
This is how google images work.
In iOS there is a very nice API SWPhotoBrowser which handles the downloading and caching of photos from the web seamlessly. I am sure there must be some API for Android as well.
If you need any help I can help with the code as well.
For Android you can use ImageLoader to download and display image - it can use cache and save memory while loading scaled images.
I want to create a main menu with a gallery that gets images from my website.
and shows them in the gallery for a user to scroll through and see upcoming event pictures.
The images will change just about every month. What is the best way to go about achieving this?
I was thinking maybe storing the images in a particular directory and having a URL set to the image.
The only question is... What happens when the images change? How would i go about updating the URL in the application? No way right?
Another thing... When the images are scrolled off the screen how do we make these images where they wont reload when out of view, causing unnecessary use of bandwidth and possibly outofmemory error.
So from what i have discovered so far, What is the best way a
You could store the urls in a database, have the app download a small text file upon launch containing the newest urls. if there is a difference, then download the new images and update the links in the database.