Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaStore - date modified don't work correctly

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

like image 878
prom85 Avatar asked Dec 23 '15 19:12

prom85


2 Answers

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

like image 174
Buddy Avatar answered Nov 02 '22 21:11

Buddy


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)
            )
        }
like image 24
Vinod Kamble Avatar answered Nov 02 '22 21:11

Vinod Kamble