1

I'm writing a purge function for our software that removes all image files that haven't been used in over a month. To do this, I am checking two things:

  1. the last-modified date of the file
  2. a query on a database that shows recently viewed files.

The query is much slower than the file check. So I would ideally like to be able to reset the last modified date on any files that fail the first check, but pass the second, so that (for example) a list of venerable but often-used files aren't gradually increasing the processing load of the management system.

Is there a way to do this without recourse to something crude (and possibly even slower), like renaming each file to a temporary label, then itself?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Andrew Wyld
  • 7,133
  • 7
  • 54
  • 96

2 Answers2

5

As Nik said the setLastModified() method on the File class may not always work depending where you are in the Android file system. If on the SD Card then some devices will return false from that call and thus not change the date.

There are more details about it here:

http://code.google.com/p/android/issues/detail?id=1992

http://code.google.com/p/android/issues/detail?id=1699

Plus some other stackoverflow thread here:

file.lastModified() is never what was set with file.setLastModified()

Community
  • 1
  • 1
elprl
  • 1,940
  • 25
  • 36
0

setLastModified() is a standard method on any Java File object that you can use to update this value.

SDK Documentation.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • 6
    Yes, though unfortunately this is rather unreliable on Android. Many devices always return false for this call and refuse to update the timestamp unless running as root: https://code.google.com/p/android/issues/detail?id=18624 (just one example, there are several other devices which do this). Always check the return code! – Nik Reiman Feb 13 '12 at 14:24