开发者

date problem (DATE_TAKEN) for images/pictures/jpg in android

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);


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));


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());


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜