How to write exif data to image in Android?
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.
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 andandroid.content.Intent
That makes your code quite a bit shorter. Just saying
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