How to write exif data to image in Android?
Asked Answered
B

3

12

I'm trying to write a User_Comment and TAG_GPS to a captured image in an Android application using the exif interface, but for some reason the tags don't seem to be appended to the image when I view the image's details in the gallery.

It seems that maybe the tags are not being written to the captured image as the file path may be wrong. I think it could be because I've written the tags to an incorrect image path.

Does anyone know if their is a problem with the way I'm writing the tags to the image?

This is the code that saves the exif data following @Charlie's changes below:

private File getOutputPhotoFile() throws IOException {
          File directory = new File(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES), getPackageName());
          if (!directory.exists()) {
            if (!directory.mkdirs()) {
              Log.e(TAG, "Failed to create storage directory.");
              return null;
            }
          }


          String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());

          File[] files = directory.listFiles();

          File exifVar =  new File(directory.getPath(), "IMG_" + timeStamp + ".jpg");
          if(files.length!=0) {
              File newestFile = files[files.length-1];
              exifVar =  new File(directory.getPath() + File.separator + newestFile.getName());
          }

          String mString = "Generic Text..";     
          ExifInterface exif = new ExifInterface(exifVar.getAbsolutePath());
          exif.setAttribute("UserComment", mString);
          exif.saveAttributes();


          exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
            String.valueOf(latituteField.toString()));

          exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, 
            String.valueOf(longitudeField.toString()));

          exif.saveAttributes();

          return exifVar; 




    }
Billowy answered 1/1, 2015 at 17:1 Comment(2)
why are you calling exif.saveAttributes multiple times? I believe that creates a new image each time. Just wonderingJacinthe
Its possible, I haven't worked on this project in a while, I think the problem may have been that the data is being saved to a temp image called "exif" and is never written to the original image.Billowy
A
5

You need first to copy the exif files located here google exif to your project, then use the following code :

    ExifInterface exif = new ExifInterface();
    exif.readExif(exifVar.getAbsolutePath());
    exif.setTagValue(ExifInterface.TAG_USER_COMMENT, mString);
    exif.forceRewriteExif(exifVar.getAbsolutePath())); 

ExifInterface used here is the new one you've just added.

Acoustic answered 6/3, 2015 at 2:6 Comment(1)
It'd be better if you can specify the data type and initialisation of variable "exifi"Kahaleel
J
2

You're using exifVar.toString(). This returns just the filename, not the path to the image. As your app is probably not in the folder with pictures, you should use exifVar.getAbsolutePath().

If you're not taking the picture at the same time you're running the program, the Path won't be right. Use this code instead:

File[] files = directory.listFiles();

if(files.length==0) {
    // No images available
    return;
}

File newestFile = files[files.length-1];

File exifVar =  new File(directory.getPath() + File.separator + newestFile.getName());

Off-Topic:

According to your huge import list:

import android.content.*;

imports

android.content.Context,
android.content.DialogInterface and
android.content.Intent

That makes your code quite a bit shorter. Just saying

Jessikajessup answered 1/1, 2015 at 19:17 Comment(1)
the exif data still doesn't get added to the image details after the above changes, do you have any ideas?Billowy
R
0

Make sure you set the write to sd permission (both static and dynamic), Also for Android 10, you need to set this: android:requestLegacyExternalStorage="true"

to the manifest application

Rodarte answered 30/8, 2020 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.