Cannot set Date Taken / DateTime tag using ExifInterface in Android

I researched and tried many options to try to get this to work, but I don't get it with it unfortunately.

What I'm trying to do is set the Date Taken tag (Tag_DateTime) to JPEG Exif data from an Android application. I already have working code to set the Latitude and Longitute tags, but I can’t get the Date tag to install for life.

Here is the code:

SimpleDateFormat fmt_Exif = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
try {
    ExifInterface exif = new ExifInterface(filePhoto.getPath());

    // Set and save the GPS and time data
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, strLat);
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, strLong);
    exif.setAttribute(ExifInterface.TAG_DATETIME, fmt_Exif.format(locLatestLocation.getTime()));
    exif.saveAttributes();

} catch (IOException e) {
    e.printStackTrace();
}
  • locLatestLocation - The location is used to get the time in milliseconds.
  • fmt_Exif - SimpleDateFormat is used to format millisecond time in the correct format for the TAG_DateTime Exif tag.
  • strLat and strLong are filled elsewhere in the correct format to set latitude and longitude tags.

- , , . , , jpeg Date Taken, , , , .

, Sanselan , - , .

- , , ?

+5
6

Android ExifInterface, , , . , , .

, EXIF, Android (IE: TIF_ ExifInterface), , .

+4

. EXIF ​​ MIT, TAG_DATETIME :

exif.setAttribute(ExifInterface.TAG_DATETIME,"2013:06:21 00:00:07");
exif.setAttribute(ExifInterface.TAG_GPS_DATESTAMP,"2013:06:21");
exif.setAttribute(ExifInterface.TAG_GPS_TIMESTAMP,"00:00:07");

:

exif 1

exif 2

, EXIF ​​/TIFF GPS, :

exif 3

, JHeader:

try {
                    JpegHeaders headers = new JpegHeaders(FakeBurst.PICTURE_PATH);
                    App1Header app1Header = headers.getApp1Header();
                    app1Header.setValue(new TagValue(Tag.IMAGEDESCRIPTION,"bla bla bla"));
                    app1Header.setValue(new TagValue(Tag.DATETIMEORIGINAL,"2013:06:21 00:00:07"));
                    app1Header.setValue(new TagValue(Tag.DATETIMEDIGITIZED,"2013:06:21 00:00:07"));
                    headers.save(true);
                    System.out.println(this+" wrote exif timestamp");
                } catch (Exception e) {
                    e.printStackTrace();
                }

onCreate:

JpegHeaders.preheat();

:

exif 4

exif 5

, , , Android ExifInterface, , SDK, , JHeader .

+6

. TAG_DATETIME , , take_date . @hide getDateTime() getGpsDateTime(). , getGpsDateTime(). TAG_GPS_DATESTAMP TAG_GPS_TIMESTAMP.

/**
 * Returns number of milliseconds since Jan. 1, 1970, midnight UTC.
 * Returns -1 if the date time information if not available.
 * @hide
 */
public long getGpsDateTime() {
    String date = mAttributes.get(TAG_GPS_DATESTAMP);
    String time = mAttributes.get(TAG_GPS_TIMESTAMP);
    if (date == null || time == null) return -1;

    String dateTimeString = date + ' ' + time;
    if (dateTimeString == null) return -1;

    ParsePosition pos = new ParsePosition(0);
    try {
        Date datetime = sFormatter.parse(dateTimeString, pos);
        if (datetime == null) return -1;
        return datetime.getTime();
    } catch (IllegalArgumentException ex) {
        return -1;
    }
}
+2

, ExifInterface.java

private static SimpleDateFormat sFormatter;

    static {
        sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        sFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

:

exif.setAttribute(ExifInterface.TAG_DATETIME,
                            sFormatter.format(new Date(System.currentTimeMillis())));
+1
SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

exifInterface.setAttribute("DateTimeOriginal", dateTimeFormat.format(date));
exifInterface.setAttribute("DateTimeDigitized", dateTimeFormat.format(date));

( , API 14 +)

0

I had a problem with the fact that images from Nexus 6 and Lumia 935 do not have CreateDate and DateTimeOriginal fields .

Android and Windows phone will also save the time stamp on the file name IMG_20160320_145825.jpg WP_20160328_13_40_42_Pro.jpg

I will be able to fix this problem and add a field using this script:

#! /bin/bash

COUNTER=0
for filename in ./*
do
    case "$filename" in
        *.JPG|*.jpeg|*.jpg)
            COUNTER=$[$COUNTER +1]
            y=${filename:6:4}
            M=${filename:10:2}
            d=${filename:12:2}
            H=${filename:15:2}
            m=${filename:17:2}
            s=${filename:19:2}
            echo "'"$y":"$M":"$d $H":"$m":"$s"'" " --> "$filename
            exiftool -v2 -AllDates="'"$y":"$M":"$d" "$H":"$m":"$s"'" -overwrite_original $filename
            ;;
        *)
            echo $filename 'Not a *.jpg'
            ;;
    esac
done

echo $COUNTER "files updated"

I hope this helps someone

-1
source

All Articles