2

I have gonethrough the following answered question in Stackoverflow link. which has this codes as shown below.

Here I'm interested on the DATE_TAKEN part of the image. I have tried this code and it is working perfectly except for the date. It is giving out some numbers in the logcat... For eg: An image whose' date_taken is 26th November 2007, the date shown Log.i is "1196066358000". Is there any way to parse this back to a real date format.

String[] projection = new String[]{
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_TAKEN
    };

    // Get the base URI for the People table in the Contacts content provider.
    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    // Make the query.
    Cursor cur = managedQuery(images,
            projection, // Which columns to return
            "",         // Which rows to return (all rows)
            null,       // Selection arguments (none)
            ""          // Ordering
            );

    Log.i("ListingImages"," query count="+cur.getCount());

    if (cur.moveToFirst()) {
        String bucket;
        String date;
        int bucketColumn = cur.getColumnIndex(
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

        int dateColumn = cur.getColumnIndex(
            MediaStore.Images.Media.DATE_TAKEN);

        do {
            // Get the field values
            bucket = cur.getString(bucketColumn);
            date = cur.getString(dateColumn);

            // Do something with the values.
            Log.i("ListingImages", " bucket=" + bucket 
                   + "  date_taken=" + date);
Community
  • 1
  • 1
abat
  • 655
  • 1
  • 9
  • 19

4 Answers4

3

If you were to read the MediaStore documentation you would see that DATE_TAKEN is defined as 'The date & time that the image was taken in units of milliseconds since jan 1, 1970.'

You can convert this value to a human-readable format by using the Calendar and Date classes.

Integer dateTaken = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(dateTaken);
Date date = calendar.getTime();
Log.d("TAG", "Date taken = " + date.toString());
Clyde
  • 7,389
  • 5
  • 31
  • 57
Denny Weinberg
  • 2,492
  • 1
  • 21
  • 34
3

Abat,

Perhaps that value you are getting is in UTC millis since the epoch. To convert that to a readable format try the following:

    Calendar myCal;
    myCal.setTimeInMillis(milliVal);
    Date dateText = new Date(myCal.get(Calendar.YEAR)-1900,
            myCal.get(Calendar.MONTH),
            myCal.get(Calendar.DAY_OF_MONTH),
            myCal.get(Calendar.HOUR_OF_DAY),
            myCal.get(Calendar.MINUTE));
    Log.d("MyApp", "DATE: " + android.text.format.DateFormat.format("MM/dd/yyyy hh:mm", dateText));
Will Tate
  • 33,439
  • 9
  • 77
  • 71
  • 5
    thanks for replying. The date taken was in milliseconds... The problem in my code was that I used "String" type to retrieve the millisecond value. Everything became fine when I changed it to "long" type. The codes that I used are as follows: long longDate = cur.getLong(dateColumn); Date d = new Date(longDate); java.text.DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy h:mmaa"); String dateString = formatter.format(d); // Using simple date format I could retrieve the exact date – abat Mar 22 '11 at 12:16
  • Date always return January 17 1970??Any help?? – kgandroid Sep 16 '15 at 05:41
  • @kgandroid cur.getLong(dateColumn) . If you check you pass value with long, everything is ok :) – kahonmlg Feb 25 '16 at 09:43
0

I solved the problem in the following way:

int dateindex = resultSet.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN);

Date dateText;
dateText = new Date(resultSet.getLong(dateindex));

Now you can transform the Date object into human language:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
datestr = dateFormat.format(dateText);
0

If I'm not mistaken, the DATE_TAKEN is a date in millisecond format. You should be able to use the Date structure's ( http://developer.android.com/reference/java/util/Date.html ) constructor to create an object that parses the millisecond date into something you can work with.

Roots
  • 1