My app uses images from a URL and I want to reduce resources usage for both server and client.
I wonder if the Android SDK already offers something for this (if there's some standard way to do it) or I just have to figure it out.
What I have in mind is:
download images on local storage (external or internal) when accessing content (lazy loading, progress bar) and keeping them there for next use
update images (download and replace) if changed on server (keep checksum in a database that is queried when the application is started)
in order to avoid excessive local disk usage (they are quite big), delete images for content that has not been accessed for N days. This is done while loading the app or in a background asynchronous service
Should I do this step by step or is there something that already takes care of it. Are there libraries to do this properly?
I think Picasso library is best for this, it has many capabilities and is really easy to use, its features include smooth image caching (the features you need), image processing and Async downloading from URL too. It has so many useful features.
Here is its url link
http://square.github.io/picasso/
Related
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 am building an android application which requires displaying images as a flip-view which will be retrieved from server. I have considered two approaches
Retrieving images from server URL and then displaying OR
Storing the image in db at server(MySQL) and then retrieving it from sq-lite on android application
My question is, which approach will be better considering everything (performance, etc.)?
Any other better approach is also welcome :)
This depends upon your Application and products you are going to display using Images, If they are not updated frequently then it would be better to use caching for faster user experience but if data is regulatory updated and previous data is keep getting filtered then always load using network.
Another approach you can just store latest 10-20 entries in your Database and as soon as you opens the application he can see some data and new data gets downloaded, this approach keeps users engage don't leave them your app just because every time they see loading.
For Image caching purposes there various good libraries avaible which are stable solutions for multiple images downloading as well as caching purposes E.g.
UniversalImageLoader, picasso, Volley
A good approach could be store locally images locally on demand, and keep it on cache, if the image changes on future you can invalidate your cache and download a new image.
I have used in a project a library that helps me a lot with to download the image in background, allows you also load image from cache automatically if stored previously or invalidate the cache. The library is android-query and here is an example of how you could use it.
You store images locally and load then to your app from local storage (This happens in a background thread)
You update the local storage by fetching the images in background, when needed, and then trigger the load from local storage (This also happens in a background thread)
This way you won get ANR's (application not responding) because of slow or missing internet connection, and you will be able to show images without connection at all.
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
I'm using Jake Wharton's DiskLruCache lib.
I'm curious about app performance, caching strategy, using caching both in a view and across a whole application. Most of the time, the image won't change.
Let's say for example I have a 320x320 photo on my server. I open the stream, save the image.
In my list views, I show bitmaps and in the detail, I show a larger image. Should I save a thumbnail bitmap too? Is that more efficient?
What is your experience with sharing the cache "object" across the entire app (let's say I have multiple views that might leverage the same data. What are the issues with this?
For the sake of performance and currency, what if the image changes on the server. What's the best strategy to know that it's changed? I don't have access to modified date. Only size and yet, I don't really want to query size every time either. Set a flag in the app on the server and then query the flag?
In a traditional application (if there is such a thing), what's the best practice for clearing the cache from time to time? (indent weirded out.)
(I was inspired to write this after seeing all of the performance improvements by Facebook in iOS. I don't have billions to do caching but I would like to at least be smart about it! LOL)
A lot of these answers depend on the type of app you're writing, how important image updates are (and how likely images will change, etc), and total images produced. Aside from disk caching, you should be using memory caching as well, especially in ListViews and other areas where the same images will be scrolled through repeatedly. Look at LruCache and read the Caching Bitmaps entry from Google.
320x320 is probably too big for a listview, you will likely want to create thumbnails (depending on device, and how you're implementing your listviews).
1) You should be using Disk caching fairly aggressively (how you define that is up to the app you're writing). Use the external storage directory, and if they have a few GB left over, it's not an issue if you take 100 mb for your app for instance. It can all be cleared away if it's ever needed anyway.
2) There shouldn't be an issue. Disk IO (even to a flash medium) should never be handled on the main thread. Use AsyncTasks to load the images. There can only be one main foreground activity at once anyway, and while an activity is sleeping, it shouldn't be trying to read from the disk anyway.
3) Again this depends on how you're implementing your app. You should be able to get additional information when retrieving the file (even Apache can tell your app the last modified date).
3.1) You can have a sqllite db that keeps track of how often certain images are used, and most recent read. If the most recent read is a few days old, let that image expire.
Edit: There's a library from Jake Wharton now called Picasso that I would recommend using, which handles the Network/local IO as well as memory and disk caching. Check it ou here: http://square.github.io/picasso/ . A lot of what you will need to do can be accomplished with one line: Picasso.with(this).load(imageFileURL).into(imageView);
I am making an android app which is going to need a lot of pictures. But I can't have them all in the app since it would be like 10 gigabytes or something. I have read some other questions where they say its smarter to store the url to the pictures in a database. My question is, should the image url be stored in the application itself or in the database?
The DB is a part of the application itself. Is it not? If you are talking about hard-coding , keeping them in DB is better keeping extensibility in mind but would involve extra call to DB.
Hard-coding them would be better performing (How much, you need to test that).
Alternatively you can make use of build tools by storing the urls in a configuration and replacing them at the time of build. While this solves above two problems, It adds complexity to the code.
So everything has its gives and takes, you need to decide what to chose on your requirements and priorities.
If you store the URLs in a database, you are able to manage your images easier, as you don't have to hardcode everything.
store URL in a DB will more preferable. And you can load the images using picasso library would better easier and comfortable way.
Its always better to store image url's in a database, whether that database resides on your phone, or on the server and you fetch it from there using rest services is upto you.
You can use multiple libraries for image lazy loading in your app.
[1]: https://github.com/nostra13/Android-Universal-Image-Loader "Universal Image Loader" is by far a very good third party library.
You can also add your own lazy loading algorithms by creating a basic Thread Pool.