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

Java 如何在Android中将exif数据写入图像?,java,android,exif,android-gallery,android-camera-intent,Java,Android,Exif,Android Gallery,Android Camera Intent,我正试图使用exif界面在Android应用程序中为捕获的图像编写用户注释和标记GPS,但由于某些原因,当我在图库中查看图像的详细信息时,标记似乎没有附加到图像中 由于文件路径可能错误,因此可能没有将标记写入捕获的图像。我想可能是因为我把标签写到了一个错误的图像路径上 有人知道我在图像中写入标签的方式是否有问题吗 这是在@Charlie进行以下更改后保存exif数据的代码: private File getOutputPhotoFile() throws IOException {

我正试图使用exif界面在Android应用程序中为捕获的图像编写
用户注释
标记GPS
,但由于某些原因,当我在图库中查看图像的详细信息时,标记似乎没有附加到图像中

由于文件路径可能错误,因此可能没有将标记写入捕获的图像。我想可能是因为我把标签写到了一个错误的图像路径上

有人知道我在图像中写入标签的方式是否有问题吗

这是在@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; 




    }

您正在使用
exifVar.toString()
。这只返回文件名,而不是图像的路径。由于您的应用程序可能不在包含图片的文件夹中,您应该使用
exifVar.getAbsolutePath()

如果您没有在运行程序的同时拍摄照片,则路径将不正确。请改用此代码:

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());
离题:

根据您庞大的进口清单:

import android.content.*;
进口

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

这使您的代码相当短。只需说

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

ExifInterface exif=new ExifInterface();
exif.readExif(exifVar.getAbsolutePath());
setTagValue(ExifInterface.TAG\u USER\u COMMENT,mString);
exif.forceRewriteExif(exifVar.getAbsolutePath());

这里使用的ExiFinInterface是您刚刚添加的新接口。

确保您设置了写入sd权限(静态和动态), 同样对于Android 10,您需要设置: android:requestLegacyExternalStorage=“true”


对于清单应用程序

在上述更改之后,exif数据仍然没有添加到图像详细信息中,您有什么想法吗?为什么要多次调用exif.saveAttributes?我相信每次都会创造一个新的形象。我只是想知道这是可能的,我已经有一段时间没有在这个项目上工作了,我认为问题可能是数据被保存到一个名为“exif”的临时映像中,并且从未写入原始映像。如果您可以指定数据类型和变量“exif”的初始化,那就更好了