Beata
Beata

Reputation: 73

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.

Upvotes: 5

Views: 1573

Answers (1)

Rashmi S
Rashmi S

Reputation: 11

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.

Upvotes: 1

Related Questions