Meep
Meep

Reputation: 531

Get the added/modified/taken date of the video from MediaStore

Where do I get the taken date of the video from MediaStore? I got the following fields from MediaStore.

MediaStore.Video.Media.DATE_MODIFIED
MediaStore.Video.Media.DATE_TAKEN
MediaStore.Video.Media.DATE_ADDED

Those fields returned seemly default values -

dateModified: 1477043336
dateTaken: 1477043336000
dateAdded: 1477043352

Formatted dates -

dateModified: 01/01/1970
dateTaken: 01/01/1970
dateAdded: 01/01/1970

I double checked the stock gallery > random video file and I do see the correct dates. I looked at the video columns in MediaStore and I didn't see any other columns which has correct dates.

    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    String formattedDate = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED)));

    String dateModified = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED)));
    String dateTaken = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN) * 1000L));
    String dateAdded = dateFormat.format(new Date(row.getColumnIndex(MediaStore.Video.Media.DATE_ADDED) * 1000L));

    Log.d(TAG, "dateModified: "+dateModified);
    Log.d(TAG, "dateTaken: "+dateTaken);
    Log.d(TAG, "dateAdded: "+dateAdded);

    Log.d(TAG, "dateModified: "+row.getString(row.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED)));
    Log.d(TAG, "dateTaken: "+row.getString(row.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN)));
    Log.d(TAG, "dateAdded: "+row.getString(row.getColumnIndex(MediaStore.Video.Media.DATE_ADDED)));

Upvotes: 3

Views: 5814

Answers (5)

Nick Cardoso
Nick Cardoso

Reputation: 21733

Looking at the annotations on the interface, DATE_ADDED and DATE_MODIFIED are annotated as SECONDS since the epoch, rather than milliseconds. DATE_TAKEN however is annotated as milliseconds since the epoch.

This difference in annotation explains the differences in zeroes that CommonsWare's answer notes. It also guides usage:

Since date formatters usually expect timestamps in millis, you should multiply second values by 1000 first.

Upvotes: 3

Vinod Kamble
Vinod Kamble

Reputation: 271

//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)
                )
            }

Upvotes: 4

Bilal Mustafa
Bilal Mustafa

Reputation: 740

Here is a simple function to get actual result of date format.

public String getDate(long val){
   val*=1000L;
   return new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(val));
}

Upvotes: 1

Krishna Vyas
Krishna Vyas

Reputation: 1028

Syntax for convert epoch to normal date in android as follows

long date=System.currentTimeMillis(); //current android time in epoch 

Converts epoch to "dd/MM/yyyy HH:mm:ss" dateformat Means 1477043336 = 21/10/2016 09:48:56

String NormalDate = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date(date));

Upvotes: -1

CommonsWare
CommonsWare

Reputation: 1006539

Those fields returned seemly default values

I do not know why your second one has three extra zeros at the end. But, using a Unix date converter site:

  • dateModified = 1477043336 = Fri, 21 Oct 2016 09:48:56 GMT
  • dateAdded = 1477043352 = Fri, 21 Oct 2016 09:49:12 GMT

And your dateTaken, without the zeros, is the same as dateModified. So, assuming you can figure out where your zeros came from (such as by randomly deciding to multiply the value by 1000L), you have valid timestamps.

Upvotes: 0

Related Questions