File last access time on Android

百般思念 提交于 2019-12-07 09:48:26

问题


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.


回答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.



来源:https://stackoverflow.com/questions/22407071/file-last-access-time-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!