MediaStore - date modified don't work correctly
Asked Answered
M

2

6

I retrieve following values from the media store

MediaStore.Images.Media.DATE_TAKEN
MediaStore.Images.Media.DATE_MODIFIED

And read the dates from the result like following:

int dateTakenColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
int dateModifiedColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
String dateToken  = cursor.getString(dateTakenColumn);
String dateModified = cursor.getString(dateModifiedColumn);
long lDateToken = dateToken != null ? Long.parseLong(dateToken) : 0;
long lDateModified = dateModified != null ? Long.parseLong(dateModified) : 0;

And can see following behaviour (example values):

  • lDateToken looks like following: 1450696995000 <= CORRECT
  • lDateModified looks like following: 1450696995 <= WRONG

It seems like the modification dates are all cut off. I checked the real files last modified date with a file explorer, and the values should be fine, but I always get such short numbers from my media files.

Any ideas on why this happens?

PS: checked this http://developer.android.com/reference/android/provider/MediaStore.Images.ImageColumns.html, but the modified field is not listed there...

Milwaukee answered 23/12, 2015 at 19:3 Comment(0)
I
10

DATE_TAKEN is in milliseconds since 1970. See the docs

DATE_MODIFIED is in seconds since 1970, so just multiply it by 1000 and it'll be fine. See the docs

Inherence answered 23/12, 2015 at 19:15 Comment(1)
I edited my question... I actually thought so, but I could not find any documentation on that... Did you find that out by trying it out? It's so weird that both date taken is in a different format than date modified...Milwaukee
K
3

Just multiply it by 1000 to get correct date

fun convertLongToDate(time: Long): String =
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            DateTimeFormatter.ofPattern("dd MMMM yyyy").format(
                    Instant.ofEpochMilli(time*1000)
                            .atZone(ZoneId.systemDefault())
                            .toLocalDate())
        } else {
            SimpleDateFormat("dd MMMM yyyy").format(
                    Date(time * 1000)
            )
        }
Krispin answered 24/11, 2020 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.