I need a method of getting the last time a (local) file has been accessed in android.
I don't mean file.lastModified()
, but the last time it had been opened (viewed in any app of the device).
I have bunch of files that are only viewed, not modified, and i want to delete the files that has been accessed longest time ago to free up space.
I stumbled across this piece of code using java.nio.file
package:
File file = //file from context.getExternalFilesDir(dirName)
BasicFileAttributes attr = Files.readAttributes(file.toPath(),
BasicFileAttributes.class);
long accessedAt = attr.lastAccessTime().toMillis();
Can someone confirm that this actually works and retrieve the last time the file has been accessed?
Is this even possible to achieve in android?
This code requires API level 26 and above, is there any way to so with 21 <= API level < 26 ?