im currently working on a android app that has to GET over 70 images all from different urls. I am currently using the asynctask to GET all the images. would my app load the images faster if i was to create many threads and divide task of getting 70 images between the threads like 20 on one thread and 50 on another?
I suppose you want to load all 70 images at the same time? It might depend on the speed of the internet connection but basically I guess the time to completely load all images would be the same, whether you load them sequentially or in parallel. Sequentially, each image can be loaded using the full internet bandwidth, while loading the images in parallel could only use a fraction of the bandwidth, so in the end the total amount of time will be roughly the same.
Well, I can imagine that having multiple http connections each managed by different thread may be beneficial for the performance. The best way to determine the number of threads to use (could be > 2) is to benchmark your piece of code for overall download time (i.e. between begin until all images have been loaded).
For downloading multiple images asynchronously I would also recommend using picasso library.
It has very few chances to get faster, because time of loading is due to time of the connection.
The right way to do I think, is to think about designing your application better, to avoid retrieving 70 images in one time, because I don't know your application, but do you really need to display 70 images in same time ? It would be better to load images only when you want to display it. This for 2 main reasons :
Your activity will be displayed faster
Your user may not have an unlimited data plan and is maybe not on wi-fi
I am currently using the asynctask to GET all the images. would my app
load the images faster if i was to create many threads and divide task
of getting 70 images between the threads like 20 on one thread and 50
on another?
If you are using a single task to download all your (70) images that is not a proper way for it. Of course you should use separate tasks while downloading images if you want them to be downloaded faster.(I assume there is no constraint for downloading images sequentially) If you continue using a single task to download 70 images, they are going to be downloaded one by one and it will take long.
Use a library for image downloading. It is a painful job to load remote images. I suggest you to use a library for this. I'm currently using ShutterBug. Try it.
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
In my application I have to show many images (say >1000).
All these images comes from network. For now, I am downloading all images and passing them to an adapter which was set to GridView.
My problem is downloading all images at once occupies a lot of runtime memory. So I want to change my design.
Is there any way to handle images/bitmaps in large number effectively? Like downloading 100 images, then again another 100 erasing previous 100 (as per need).
You can try these optimisations:
Decrease number of images, do you really need 100 in one go?
Download image thumbnails, do you really need full-sized non-compressed images?
Cache next images, pre-load some images before user asks for them
Profile your app, see where time is consumed
Use several threads, at least one for pulling data and one for UI
I think you need to take a look at UIL
Which is helpful to you.
right now i am uploading image in single thread using asynctask. but it is not efficient and it's takes lot of time to upload. so i am refering this link to download image using ThreadPoolExecuter. can i use this strategy while uploading image also ?
is this good practice to upload image?
is this good practice to upload image?
YES that could be an option if you don't worry about tracking of each execution (i.e when thread completed its task ).
See what google doc says
they usually provide improved performance when executing large numbers
of asynchronous tasks, due to reduced per-task invocation overhead,
and they provide a means of bounding and managing the resources,
including threads, consumed when executing a collection of tasks.
You can down- as well as up-load an image in a separate thread, that's perfectly fine.
If you want to use several threads for that, it would make no sense due to limited bandwidth (several threads would not speed-up the process) and partitioning issues (on the server).
I have a service which provides new images every 5 minutes. I'd like to know how could be the images loaded quickly on android. I was thinking about making a website with images, or an android application - what would be better?
you can use handlers to update your UI thread once your service downloads the image... if your downloading higher resolution images its better to decode it Efficiently loading Bitmap..
I am building an "Image Gallery" app that would fetch about 50 - 60 HD images from a site. The site will update the image list thrice a week and so new images will have to be fetched and old images discarded when the app is launched the next time.
What's the best way to cache these images and lazy load them without running
into OutOfMemory and SoftReference issues? Lazy loading would be needed because each Image thumbnail has a caption and freezing the thumbnail UI till the image loads is not a good idea.
Also, for lazy loading images - is it really a good design to spawn a thread for each thumbnail and implement a queue to handle the threads? Is there an easier way?
Please help!
Thanks for reading!
You can have a loot at PicHelper from Zwitscher source for a class that does the fetching and storing on the local file system, as well as reading Bitmaps from those files again.
Within the main code you would start an AsyncTask to fetch the images (in its doInBackground() method. To prevent OOME I'd put the list of images to fetch in a "queue" and fetch them in serial, rather than parallel. Many handsets out there only have 48MB of heap or less.
As the question is general, so is my attempt to help you. Some ideas:
- load images in one separate thread
- either store them as files with names on the SD card, or... download them each time the app is run!
- put them in a GridView. You can use a message handler (messages from the thread to the main UI thread) to update the GridView
- if you decide to store the images on the phone, identify them either by filenames, or store the names in local database.
Actually I have implemented a GridView for reading images on the SD card, and lazy loading images for a list, not exactly what you are trying to do, but it is definitely doable and you can probably find some code on the net.