1

I'm implementing a news page in my app. I want the page to download any new images and store them on the phone.

My question is: how do I tell which images have been downloaded and which ones haven't?

I was thinking of saving the images as the yymmddhhss.png but that seems sloppy. I saw someone use a url.hashCode() implementation but I'm not sure what it does so I'm not sure if it'll work for my application.

I'd like the implementation to use built in function OR filename tricks. I'd rather not setup a DB or preference to store the name/date of the last downloaded image.

Rawr
  • 2,206
  • 3
  • 25
  • 53
  • are you trying to cache images across launches? agree a database is heavy. assuming this is some kind of rss/atom feed, i would append the URL of the article to the filename of the image, or just use the original filename.. – speakingcode Oct 18 '12 at 04:34
  • Did you try [Fedor's](http://stackoverflow.com/users/95313/fedor) [Lazy Loading](http://stackoverflow.com/a/3068012/450534)? This solution is useful if don't really care what file names they are stored as. If that matters for some reason, then Lazy Loading won't fit exactly in your needs list. – Siddharth Lele Oct 18 '12 at 04:45
  • Why can't you just use the name of the url, taking away the illegal characters (i.e. just extract alpha numeric string of the URL for the image)? This way it will always match, always be consistent, not to mention dead easy... – dineth Oct 18 '12 at 05:37
  • try this example http://iamvijayakumar.blogspot.in/2011/06/android-lazy-image-loader-example.html – Hiren Dabhi Oct 18 '12 at 05:37
  • @Hiren Dabhi that's the example that uses `String filename=String.valueOf(url.hashCode());` which is the issues I'm trying to avoid. – Rawr Oct 18 '12 at 16:40

1 Answers1

2

Don't use hashCode(). That is Java specific. If you look more into - hashCode()

Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.


The easiest solution is to md5 the FULL URL and use that to name the file. It will give you consistent file name length and minimize the chances of conflict with other images. You don't need to add dates to the name because you can retrieve it straight from the time the file was written.

A simple example to get MD5 - Example

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
kungfoo
  • 597
  • 5
  • 16
  • with this method every image has have a new name correct? just use something like: banner1.jpg, banner2.jpg, banner3.jpg, etc – Rawr Oct 18 '12 at 15:51
  • with `md5` the chances of duplicate file names are statistically very very very small. For your purpose, you shouldn't worry about name conflict. – kungfoo Oct 19 '12 at 04:57
  • Great explanation. Exactly what I was looking for: 1) why hashCode() is a poor method 2) A good suggestion – Rawr Oct 21 '12 at 03:08