java 如何在Android中将exif数据写入图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27732781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:18:27  来源:igfitidea点击:

How to write exif data to image in Android?

javaandroidexifandroid-galleryandroid-camera-intent

提问by Brian J

I'm trying to write a User_Commentand TAG_GPSto 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.

我正在尝试使用 exif 界面在 Android 应用程序中写入User_CommentTAG_GPS捕获的图像,但由于某种原因,当我在图库中查看图像的详细信息时,标签似乎没有附加到图像中。

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:

这是以下@Charlie 更改后保存 exif 数据的代码:

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; 




    }

回答by user51465478484

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

您首先需要将位于google exif的 exif 文件复制到您的项目中,然后使用以下代码:

    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.

此处使用的 ExifInterface 是您刚刚添加的新接口。

回答by Charlie

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().

你正在使用exifVar.toString(). 这仅返回文件名,而不是图像的路径。由于您的应用程序可能不在带有图片的文件夹中,您应该使用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

这会让你的代码更短一些。只是说