How to write exif data to image in Android?

北战南征 提交于 2019-11-30 06:57:08

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

ExifInterface exif = new ExifInterface();
exifi.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 and
android.content.Intent

That makes your code quite a bit shorter. Just saying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!