10

I'm trying to write lat-long and other data to the Exif header of a jpeg in my custom camera app. Typically android automatically populates the header with data such as aperture, ISO, shutter speed etc. However, when I manually add create an ExifInterface instance, set the GPS location with SetAttributes(), and call SaveAttributes(); all of the other camera data dissapears.

Is this supposed to happen? How can I simply add a tag without overwriting everything else?

I saw an example elsewhere of creating two ExifInterfaces, an old(from the picture) and a new, and copying every populated value from the old to the new along with any other data. This however, is annoying and lengthy. I would like to find a better solution.

Here is my code:

try{ 
    ExifInterface exif = new ExifInterface(pictureFile.getAbsolutePath());
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, mGpsLocation.getLatDms());
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, mGpsLocation.getLonDms());
    exif.setAttribute(ExifInterface.TAG_GPS_ALTITUDE, mGpsLocation.getAltDms());
    exif.saveAttributes(); 
} catch(IOException e){
    e.printStackTrace(); 
}

Thanks

zmarties
  • 4,809
  • 22
  • 39
JohannB
  • 357
  • 6
  • 21
  • I am not sure why this is happening with you. I have tried the same method you have mentioned and none of my EXIF data is overwritten. Only my GPS location is modified after saving attributes. Tested on following devices, Nexus Prime - 4.2.1 Motorola Moto E - 4.4.4 – Ajith M A Jan 06 '15 at 11:38

1 Answers1

0

From the documentation I've seen this this is not supposed to happen. http://developer.android.com/reference/android/media/ExifInterface.html

public void saveAttributes ()

Added in API level 5
Save the tag data into the JPEG file. This is expensive because it involves copying all the JPG data from one file to another and deleting the old file and renaming the other. It's best to use setAttribute(String, String) to set all attributes to write and make a single call rather than multiple calls for each attribute.

Throws
IOException 

It clearly says that it's copying ALL the data, including the things that you say is vanishing. Could you maybe post what you are testing it on. If you are on Android Studio you could try File -> Invalidate Caches / Restart -> Invalidate and Restart.

Alternatively I found an answer to a similar question about losing the data here: https://stackoverflow.com/a/13784057/3585278

As others have indicated, you must copy the EXIF data from the original image to the final resized image. The Sanselan Android library is typically best for this. Depending on Android OS version, the ExifInterface sometimes corrupts the EXIF data. In addition, the ExifInterface also handles a limited number of Exif tags -- namely only the tags that it "knows" about. Sanselan on the other hand will keep all EXIF tags and marker notes. Here is a blog post that shows how to use Sanselan for copying image data: http://bricolsoftconsulting.com/copying-exif-metadata-using-sanselan/ BTW, on Android I also tend to rotate the images and remove the Orientation EXIF tag. For example, on a Nexus S with Android 4.03, the camera was setting an orientation tag in the EXIF metadata, but the webview was ignoring that info and displaying the image incorrectly. Sadly, rotating the actual image data and removing the EXIF orientation tag is the only way to get every program to display images correctly.

I hope this helps.

Community
  • 1
  • 1
Danieboy
  • 4,393
  • 6
  • 32
  • 57