复制 exif 标签时出现 Android NullPointerException

标签 android image bitmap nullpointerexception exif

我正在尝试调整图像大小,该部分已完成。 然后我尝试将 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 信息:

当我尝试读取这两个文件时,会创建新文件: enter image description here


在 oldexif 上调试监视 enter image description here


在 newexif 上调试监视 enter image description here


测试图像

http://vikaskanani.files.wordpress.com/2011/07/test.jpg


使用 SDK 2.1 的 Android 模拟器

最佳答案

您正在 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 行包含以下内容:

sb.append(val.length() + " ");

这里唯一可以为 null 的是 val。 (您在 saveAttributes 方法中传递的值之一)。

我建议仔细检查您传递给该值的值,并注意空值。

关于复制 exif 标签时出现 Android NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812711/

相关文章:

android - 将 Stack Widget 的内容转换为图像

android - 在 OpenGL ES 和 Android 中处理大型位图

c++ - 如何在 C++ 中将位图 16 位 RGBA4444 转换为 16 位灰度?

android - 有没有人使用 Android NDK 构建 ZBar?

java - 新建的 SQLitedatabase 表不存在

image - 检测图像中的乐高底板

c# - int rosu=Color.red.getRGB() 从 Java 到 C#

android - 通过Camera API 2捕获图片并保存为Bitmap

android - 关闭 DialogFragment 时布局拉伸(stretch)

android - 如何在 android phonegap 中测试推送通知?