且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android中写入EXIF数据图像?

更新时间:2023-11-09 14:10:10

您使用的是 exifVar.toString()。这返回刚刚的文件名,而不是路径到图像。当你的应用程序可能无法与图片的文件夹中,你应该使用 exifVar.getAbsolutePath()

如果你没有拍摄照片的同时,你正在运行的程序,路径不会是正确的。使用此code来代替:

 文件[]文件= directory.listFiles();

如果(files.length == 0){
    //没有可用的图片
    返回;
}

文件newestFile =文件[files.length-1];

文件exifVar =新的文件(directory.getPath()+文件分割符+ newestFile.getName());
 

题外话:

根据您的庞大的进口清单:

 进口android.content *。
 

进口

  android.content.Context,
android.content.DialogInterface和
android.content.Intent
 

这使你的code颇有几分短。只是说

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; 




    }

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