Is there a way to get lastAccess time for a file in Android. I know how to do it using java nio.file package, however, Android's support for java 7 is very limited and does not include the file package. I am not interested in lastModified, only lastAccessed, as I would like to delete the oldest read files.
Asked
Active
Viewed 1,479 times
5
-
If nothing else you could write a small NDK wrapper around stat(), however the kernel can only give you this information if the file system on which it resides actually tracks it, which may or may not be the case. – Chris Stratton Mar 14 '14 at 14:22
-
Thank you, but I believe stat command is not available on Android. – Beata Mar 14 '14 at 19:05
-
It might not be, but I wasn't talking about the stat *command* but rather the stat() *system call* which is most definitely available. See linuxmanpages.com/man2/stat.2.php for example, though Android's bionic libc defines the struct in `
` differently, using 64 bit types and apparently actually uses the kernel's stat64(). – Chris Stratton Mar 14 '14 at 19:19 -
@ChrisStratton Is it possible to do it via Java, using the Runtime.getRuntime().exec(...) ? Is it maybe possible by using root? – android developer Jul 02 '14 at 20:39
-
@ChrisStratton - Were you able to fetch last accessed time for files in android ? – Jaydev May 25 '16 at 18:42
-
I explained how it could be done for file systems where the information exists, but I had no reason to implement it myself. – Chris Stratton May 25 '16 at 18:44
1 Answers
1
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
StructStat stat = null;
try {
stat = Os.stat("/sdcard/Pictures/abc.jpg"); // File path here
} catch (ErrnoException e) {
e.printStackTrace();
}
long t2 = stat.st_atime *1000L;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t2);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh-MM-ss");
String formattedDate = formatter.format(calendar.getTime());}
This works for only above Lollipop APIs though.
Also note that st_atime returns time in seconds and not in milliseconds.

Rashmi S
- 11
- 2