Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File last access time on Android

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.

like image 765
Beata Avatar asked Nov 11 '22 11:11

Beata


1 Answers

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.

like image 187
Rashmi S Avatar answered Nov 14 '22 22:11

Rashmi S