Android Last File Access Time

Is there any way to get lastAccess time for a file in Android. I know how to do this using the java nio.file package, however Android support for java 7 is very limited and does not include the file package. I'm not interested in lastModified, only lastAccessed, since I would like to delete old files for reading.

+4
source share
1 answer
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 only works for the Lollipop API.

Also note that st_atime returns time in seconds, not milliseconds.

+1
source

Source: https://habr.com/ru/post/1531779/


All Articles