复制 exif 标签时出现 Android NullPointerException
我正在尝试重新调整图像大小,该部分已完成。 然后我尝试将 exif 标签复制到新文件。 我使用 ExifInterface 来读取标签。 我知道它是一个接口而不是一个对象。 但是当我尝试将它用于非常大尺寸的图像时,我得到了 NullPointerException 。 并非所有图像都会出现此错误。
07-25 11:59:23.870: WARN/System.err(1362): java.lang.NullPointerException
07-25 11:59:23.870: WARN/System.err(1362): at android.media.ExifInterface.saveAttributes(ExifInterface.java:202)
我该如何解决?
复制 exif 信息的代码
try {
// copy paste exif information from original file to new
// file
ExifInterface oldexif = new ExifInterface(filePath);
ExifInterface newexif = new ExifInterface(file.getPath());
int build = Build.VERSION.SDK_INT;
// From API 11
if (build >= 11) {
newexif.setAttribute("FNumber",
oldexif.getAttribute("FNumber"));
newexif.setAttribute("ExposureTime",
oldexif.getAttribute("ExposureTime"));
newexif.setAttribute("ISOSpeedRatings",
oldexif.getAttribute("ISOSpeedRatings"));
}
// From API 9
if (build >= 9) {
newexif.setAttribute("GPSAltitude",
oldexif.getAttribute("GPSAltitude"));
newexif.setAttribute("GPSAltitudeRef",
oldexif.getAttribute("GPSAltitudeRef"));
}
// From API 8
if (build >= 8) {
newexif.setAttribute("FocalLength",
oldexif.getAttribute("FocalLength"));
newexif.setAttribute("GPSDateStamp",
oldexif.getAttribute("GPSDateStamp"));
newexif.setAttribute("GPSProcessingMethod",
oldexif.getAttribute("GPSProcessingMethod"));
newexif.setAttribute("GPSTimeStamp",
oldexif.getAttribute("GPSTimeStamp"));
}
newexif.setAttribute("DateTime",
oldexif.getAttribute("DateTime"));
newexif.setAttribute("Flash", oldexif.getAttribute("Flash"));
newexif.setAttribute("GPSLatitude",
oldexif.getAttribute("GPSLatitude"));
newexif.setAttribute("GPSLatitudeRef",
oldexif.getAttribute("GPSLatitudeRef"));
newexif.setAttribute("GPSLongitude",
oldexif.getAttribute("GPSLongitude"));
newexif.setAttribute("GPSLongitudeRef",
oldexif.getAttribute("GPSLongitudeRef"));
// You need to update this with your new height width
newexif.setAttribute("ImageLength",
oldexif.getAttribute("ImageLength"));
newexif.setAttribute("ImageWidth",
oldexif.getAttribute("ImageWidth"));
newexif.setAttribute("Make", oldexif.getAttribute("Make"));
newexif.setAttribute("Model", oldexif.getAttribute("Model"));
newexif.setAttribute("Orientation",
oldexif.getAttribute("Orientation"));
newexif.setAttribute("WhiteBalance",
oldexif.getAttribute("WhiteBalance"));
newexif.saveAttributes();
Toast.makeText(getApplicationContext(),
"Image resized & saved successfully",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
附加 onFly 信息:
当我尝试读取这两个文件时,会创建新文件:
在 oldexif 上调试监视
在 newexif 上调试监视
测试图像
http://vikaskanani.files.wordpress.com/2011/07/test.jpg
使用 Android 模拟器作为 sdk 2.1
I am trying to re-size an image, that part completed.
Then I'm trying to copy exif tags to new file.
I use ExifInterface to read tags.
I know it's an interface not an object.
But When I try to use it for really big sized image, I get NullPointerException
.
I get this error not for all the images.
07-25 11:59:23.870: WARN/System.err(1362): java.lang.NullPointerException
07-25 11:59:23.870: WARN/System.err(1362): at android.media.ExifInterface.saveAttributes(ExifInterface.java:202)
How do I solve it?
Code to copy exif information
try {
// copy paste exif information from original file to new
// file
ExifInterface oldexif = new ExifInterface(filePath);
ExifInterface newexif = new ExifInterface(file.getPath());
int build = Build.VERSION.SDK_INT;
// From API 11
if (build >= 11) {
newexif.setAttribute("FNumber",
oldexif.getAttribute("FNumber"));
newexif.setAttribute("ExposureTime",
oldexif.getAttribute("ExposureTime"));
newexif.setAttribute("ISOSpeedRatings",
oldexif.getAttribute("ISOSpeedRatings"));
}
// From API 9
if (build >= 9) {
newexif.setAttribute("GPSAltitude",
oldexif.getAttribute("GPSAltitude"));
newexif.setAttribute("GPSAltitudeRef",
oldexif.getAttribute("GPSAltitudeRef"));
}
// From API 8
if (build >= 8) {
newexif.setAttribute("FocalLength",
oldexif.getAttribute("FocalLength"));
newexif.setAttribute("GPSDateStamp",
oldexif.getAttribute("GPSDateStamp"));
newexif.setAttribute("GPSProcessingMethod",
oldexif.getAttribute("GPSProcessingMethod"));
newexif.setAttribute("GPSTimeStamp",
oldexif.getAttribute("GPSTimeStamp"));
}
newexif.setAttribute("DateTime",
oldexif.getAttribute("DateTime"));
newexif.setAttribute("Flash", oldexif.getAttribute("Flash"));
newexif.setAttribute("GPSLatitude",
oldexif.getAttribute("GPSLatitude"));
newexif.setAttribute("GPSLatitudeRef",
oldexif.getAttribute("GPSLatitudeRef"));
newexif.setAttribute("GPSLongitude",
oldexif.getAttribute("GPSLongitude"));
newexif.setAttribute("GPSLongitudeRef",
oldexif.getAttribute("GPSLongitudeRef"));
// You need to update this with your new height width
newexif.setAttribute("ImageLength",
oldexif.getAttribute("ImageLength"));
newexif.setAttribute("ImageWidth",
oldexif.getAttribute("ImageWidth"));
newexif.setAttribute("Make", oldexif.getAttribute("Make"));
newexif.setAttribute("Model", oldexif.getAttribute("Model"));
newexif.setAttribute("Orientation",
oldexif.getAttribute("Orientation"));
newexif.setAttribute("WhiteBalance",
oldexif.getAttribute("WhiteBalance"));
newexif.saveAttributes();
Toast.makeText(getApplicationContext(),
"Image resized & saved successfully",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
Additional onFly information:
New file is created when I'm trying to read both the file:
Debug watch on oldexif
Debug watch on newexif
Testing image
http://vikaskanani.files.wordpress.com/2011/07/test.jpg
Using Android emulator for sdk 2.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在 saveAttributes 中的某处传递空值。
只要您有源代码,调试 NullPointerExceptions 就非常容易。
您可以在下面找到适用于 Android 2.1 的 ExifInterface 的源代码
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/media/ExifInterface.java#ExifInterface
第 202 行包含以下内容:
这里唯一可以为 null 的是 val。 (您在 saveAttributes 方法中传递的值之一)。
我建议仔细检查您传递给该值的值,并注意空值。
You are passing a null value somewhere in the saveAttributes.
Debugging NullPointerExceptions is very easy, providing you have the sourcecode.
Below you can find the sourcecode for the ExifInterface for Android 2.1
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/media/ExifInterface.java#ExifInterface
line 202 contains this :
Only thing that can be null here is val. (one of the values you're passing in the saveAttributes method).
I would suggest double checking the values you're passing onto that value, and watch out for null values.
复制 EXIF 数据的更好方法是使用 Sanselan Android 库。 ExifInterface 在某些版本上存在数据损坏错误,并且还处理有限数量的 EXIF 标签。
这是博客文章,讨论使用 Sanselan 复制 EXIF 数据并提供示例代码:
http://bricolsoftconsulting.com/copying-exif-metadata-using-sanselan/
A better approach to copying EXIF data is to use the Sanselan Android library. The ExifInterface has data corruption bugs on some versions, and also handles a limited number number of EXIF tags.
Here is blog post that talks about copying EXIF data using Sanselan and provides sample code:
http://bricolsoftconsulting.com/copying-exif-metadata-using-sanselan/