我正在尝试调整图像大小,该部分已完成。 然后我尝试将 exif 标签复制到新文件。 我使用 ExifInterface 来读取标签。 我知道它是一个接口(interface)而不是一个对象。 但是当我尝试将它用于非常大尺寸的图像时,我得到了 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
使用 SDK 2.1 的 Android 模拟器
最佳答案
您正在 saveAttributes 中的某处传递空值。
只要您有源代码,调试 NullPointerExceptions 就非常容易。
您可以在下面找到适用于 Android 2.1 的 ExifInterface 的源代码
第 202 行包含以下内容:
sb.append(val.length() + " ");
这里唯一可以为 null 的是 val。 (您在 saveAttributes 方法中传递的值之一)。
我建议仔细检查您传递给该值的值,并注意空值。
关于复制 exif 标签时出现 Android NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812711/