Android Question Cache image in disk

Emme Developer

Well-Known Member
Licensed User
Longtime User
Hi to all!
I've an image in a server, with an unique link. I want to display this image inside an imageview. I download it and after display it, i also save it in cache folder. When i re-enter in the page, if the image is in cache then i display it. Now i want to mantain updated the image, as it sometimes change, but i want avoid to download the image every time i go in the page. Link need to be the same, so i can't change it. How can i setup this case? I saw something like Etag in http, but seems it is not fully supported in OkHttp and Android. I though i can use a php to check the filesize in server and in cache, and download the image only if them are different, but the filesize is not the same (maybe different file size compression in different system?). Any ideas?

PS: i know there are Picasso and Glide, but i need to do a "manual" solution as i need to port it in iOS
Many thanks!
 

OliverA

Expert
Licensed User
Longtime User
Something like:

Create a hash for the image on the server and put that hash in a page on the server that can be accessed. Anytime the picture changes on the server, update the hash.

On the client, if you download the picture for the first time, download it and create a hash (using same hashing function) of the picture. Later on, before displaying the picture on the client, request the page on the server that has the hash and compare it to your client hash. If it is not the same has, download the picture, otherwise just display the local copy of the picture.

This would probably only be worth it if the picture is of some significant size or download speeds are abysmally slow (or combo).
 
Last edited:
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Maybe this is a good solution, many thanks. I can use meta tag on image to avoid to mantain 2 file and generate the metatag with the hash in upload operation
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…