Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant set Date Taken/DateTime tag using the ExifInterface in Android

I've researched and tried numerous options to try and get this to work, but am not getting anywhere with this unfortunately.

What I am trying to do is set the Date Taken tag (Tag_DateTime) in a JPEG's Exif data from within an Android app. I already have working code to set the Latitude and Longitute tags, but cannot for the life of me get the Date taken tag to set.

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 - Location being used to get the time in milliseconds.
  • fmt_Exif - SimpleDateFormat used to format the millisecond time into the correct format for the TAG_DateTime Exif tag.
  • strLat & strLong - Populated elsewhere in the correct format to set the Latitude and Longitude tags.

I read in a post somewhere that the tag needs to be written in the milliseconds format, so have tried this too to no avail. In order to confirm my formatting with what is actually stored, I read and outputted the unformatted tag from a jpeg file which has the Date Taken tag, but the output is in exactly the same format as what I am writing to the tag and its still not working.

It might also be worth mentioning that I have looking into the Sanselan class to do this, and due to the complexity and lack of examples would much rather try and get my existing solution working before changing to a completely different one.

Has anyone managed to do this and if so what am I doing wrong??

like image 683
JohnHenry Avatar asked Jan 25 '12 14:01

JohnHenry


1 Answers

I needed to the same thing just now. I've read this EXIF article from MIT and I got TAG_DATETIME to be written:

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

Which looked like this previewed:

exif 1

exif 2

Note that this in the EXIF/TIFF and GPS sections only, not the actual Original and Digitized timestamps:

exif 3

I wanted to change the Original and Digitized timestamps too, so tried the JHeader library:

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

with this added in onCreate:

JpegHeaders.preheat();

and it worked:

exif 4

exif 5

I see this post is from December so maybe the Android ExifInterface code I posted above didn't work with that version of the SDK, but I'm guessing the JHeader library approach would work.

like image 183
George Profenza Avatar answered Oct 06 '22 14:10

George Profenza