如何在Android中将exif数据写入图像? [英] How to write exif data to image in Android?
问题描述
我正在尝试使用 exif 界面将 User_Comment
和 TAG_GPS
写入 Android 应用程序中捕获的图像,但由于某种原因标签似乎没有当我在图库中查看图像的详细信息时附加到图像中.
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?
这是下面@Charlie 更改后保存exif 数据的代码:
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;
}
推荐答案
您首先需要复制位于此处的 exif 文件 google exif 到您的项目,然后使用以下代码:
You need first to copy the exif files located here google exif to your project, then use the following code :
ExifInterface exif = new ExifInterface();
exif.readExif(exifVar.getAbsolutePath());
exif.setTagValue(ExifInterface.TAG_USER_COMMENT, mString);
exif.forceRewriteExif(exifVar.getAbsolutePath()));
这里使用的 ExifInterface 是您刚刚添加的新接口.
ExifInterface used here is the new one you've just added.
这篇关于如何在Android中将exif数据写入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!