I am creating an application with lots of photos. They will take > 100 mb on memory. So far I have 3 ideas:
Place it in #drawable - app will be heavy
Download by JSON and place in java array - cashing - How long it can be there?
Download by JSON, place it on phone memory/SD card and read in application from memory. User can Remove pictures.
The best option will be to give possibility to user if he want to have a pictures or not.
What strategy is most efficient and recommended?
Every opinion and tutorial will be helpfull.
Thanks
There are a lot of library that do the work of downloading the images from web and then cache them in your preferred memory for you. Some of them would be:
1. nostra13 / Android-Universal-Image-Loader
2. Square / Picasso
3. Google / Volle
I wrote a blog post on this subject that you can check:
How to load images asynchronously into a ListView
Check out Android Image Loader, it can also cache the images for you to prevent re-downloads in the future. It has many other features also.
Easy to use too, can load an image from a url into a view by simply using:
imageLoader.displayImage(imageUri, imageView);
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
Lets consider the following, I have an app with several dozen photos I want to show the user at any given time.
Right now I'm creating multiple files for each image, sizing them for different screen sizes and storing them in their respective drawable folders.
It's increasing the size of my app dramatically.
So here is my question: Is it possible to store the images on a server and use an image library like Picasso, Fresco or something else (open to anything) to fetch that image and scale it down for the device it's running on without risking running out of memory?
I'm worried that fetching a large image, loading it into memory and then resizing it will cause the same problem as trying to display it on older devices with little memory available to them.
You can write methods to request different images sizes from your server based on client info. Just write a method to measure the screen size and then request the appropriate image based on a URL endpoint (like http://myimageserver.com/images/ldpi/image1.png).
You can do optimization post-download, such as scaling, before saving the image to a local file store.
Using a reputable image loading library is a valid method (my own favourite is Glide).
The answer to your question really depends on the number of images you want to show! If there are lots, then yes storing them on a server is probably best, but also the most time-consuming and expensive (both in time and money).
Your other (easier) option is to keep the originals in the assets folder, and use your image loader to scale and load them for you. The correct path for an image in your assets folder is file:///android_asset/your_image_here.jpg. This way, you're only keeping one version of each photo in your apk and they'll load much faster.
I want to save the images fetched from server for once and from next time i want to check first whether images are stored or not in device, if not then again it should fetch from server and store in user's device again, and if yes then application will use images directly rather than fetching from server again and again. It will be useful for enhancing the speed of application. Basically my application is fetching multiple images from server so i want to save those images on user's android device and from next time application should fetch from device. I think you got my question.
The simple way:
You can use Picasso.
It is a simple lib which provides image downloading and caching.
In my opinion it might not be the fastest, but it is pretty simple and intuitive. It does its job well and none who I asked complained about it.
Picasso
Other libs:
UIL
Volley
Glide
fresco
To make it short. There are lots of other libs. An awesome comparision of the most Populat ones can be found here and here
The do it yourself way:
You can also write you own caching logic with a LRUCache. Which is also pretty simple.
Take a look at:
https://developer.android.com/topic/performance/graphics/cache-bitmap.html
The LRUCache is just a Memory Cache so you might also want to use a DiskLRUCache
I have an app where people can set pictures as wallpaper but its really slow because pictures are very big. This app load pictures and then when i want to see it again it has to load again.
What to do to make it faster? I use Json service. My pictures are from server. i am using universal image loader.
Theres 2 types of cache in android, the memory cache and the disk cache. You should use picasso its a library that implement both and is very easy to use (1 line of code).
I'm making an android app, here the images are getting from Cloud, is it good idea to download images and save it & use it further. Or download images every-time user uses the app, what idea you prefer is the best?
Because downloading images always is slow & its bad i know but at some point if the images are updated then how to get to know about it?
You should definitely cache your downloaded files!
Do it in your internal app directory where only you do have access to (or otherwise external storage, thats still ok).
Bandwidth and connections are always expensive and should kept low as much as possible.
So your user can see images fast even on a bad connection and your app doesn't waste his valuable bandwidth of a users data plan.
Maybe this could also help you:
https://github.com/novoda/ImageLoader
http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
Make it easy on yourself and use something like Android Smart Image View. It takes care of loading and caching, and it's just about a drop-in replacement for Android's ImageView. Universal Image Loader is another alternative, more configurable, but not as quick to implement.
I used https://github.com/nostra13/Android-Universal-Image-Loader
but I think you not want only download and cache.
these no trick ,if you want check weather the image update or not, you can add metadata for image, just like md5 .
in html and browser, you can set expires header for a image:
enter link description here
but in android app, you control all yourself.
Downloading images and saving them is probably the best way to do it because you don't want to download the same images over and over. If the images are updated you can delete the older one and download the new ones. Just make sure you don't download/save a million images. Take a look at this library. It has a built-in cache on sdcard/external sd.
Downloading images from the net for display, with possible requirement of caching is a very common problem that many people have solved, you can try these solutions to see which fits you:
Ion (https://github.com/koush/ion) - very flexible and feature complete, plus it can download more than images but JSON, Strings, Files, and Java types as well. The part that I really like about this is that it can automatically cancel operations when the calling Activity finishes, so users don't waste time & bandwidth downloading images that will no longer be displayed
Universal Image Loader (https://github.com/nostra13/Android-Universal-Image-Loader) - equally capable for most use cases but for downloading/caching images only