如何在Android中写入EXIF数据图像? [英] How to write exif data to image in Android?

查看:474
本文介绍了如何在Android中写入EXIF数据图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个 User_Comment TAG_GPS 在Android应用程序中所拍摄的图像使用EXIF写接口,但由于某些原因的标签似乎并没有被添加到图像时,我认为在画廊图像的细节。

看起来也许标签不被写入到所捕获的图像作为文件路径可能是错误的。我想这可能是因为我写的标签不正确的图片路径。

有谁知道,如果他们是我写的标签形象?

方式有问题

这是在code,节省下@查理的变化以下EXIF数据:

 私人文件getOutputPhotoFile()抛出IOException异常{
          文件目录=新的文件(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES),getPackageName());
          如果(!directory.exists()){
            如果(!directory.mkdirs()){
              Log.e(TAG,无法创建存储目录。);
              返回null;
            }
          }


          字符串的timeStamp =新的SimpleDateFormat(yyyMMdd_HHmmss,Locale.ENGLISH).format(新日期());

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

          文件exifVar =新的文件(directory.getPath(),IMG_+的timeStamp +.JPG);
          如果(files.length!= 0){
              文件newestFile =文件[files.length-1];
              exifVar =新的文件(directory.getPath()+文件分割符+ newestFile.getName());
          }

          字符串mString =通用文字。;
          ExifInterface EXIF​​ =新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();

          返回exifVar;




    }
 

解决方案

您使用的是 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

这篇关于如何在Android中写入EXIF数据图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆