5

有没有办法在 Android 中获取文件的 lastAccess 时间。我知道如何使用 java nio.file 包来做到这一点,但是,Android 对 java 7 的支持非常有限,并且不包括文件包。我对 lastModified 不感兴趣,只对 lastAccessed 感兴趣,因为我想删除最旧的读取​​文件。

4

1 回答 1

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());}

不过,这仅适用于上述 Lollipop API。

另请注意, st_atime 以秒而不是毫秒为单位返回时间。

于 2016-05-31T14:23:57.247 回答