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.
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.
来源:https://stackoverflow.com/questions/22407071/file-last-access-time-on-android