Is it possible to use reset the last modified date of an Android file?

I am writing a cleaning function for our software that deletes all image files that have not been used for a month. To do this, I check two things:

  • file last modified date
  • A query in the database that shows recently viewed files.

The request is much slower than checking the file. Therefore, I would ideally like to be able to reset the last modified date on any files that did not pass the first check, but transfer the second, so that (for example) a list of venerable but often used files will not gradually increase the loading capacity of the management system.

Is there any way to do this without using some kind of coarse (and possibly even slow), for example, renaming each file to a timestamp, and then itself?

+1
source share
2 answers

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

SDK documentation .

+1
source

As Nick said, the setLastModified() method in the File class may not always work depending on where you are in the Android file system. If on the SD card some devices return false from this call and thus do not change the date.

There is more detailed information here:

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

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

Plus a few other stackoverflow threads here:

file.lastModified () was never installed with file.setLastModified ()

+5
source

Source: https://habr.com/ru/post/953402/