From 43b0a458ab1136e9f13eb8f5fd967523082a5bda Mon Sep 17 00:00:00 2001 From: Michal Piechowski Date: Wed, 15 Apr 2015 13:03:50 +0200 Subject: [PATCH] Don't use timestamps with all zero Although all zero (0000:00:00) is valid time, in most cases it means that value is not present. According to http://www.exiv2.org/Exif2-2.PDF in such case those values should be omitted, however some cameras set them to 0 anyway. With this commit such timestamps will be treated as they were empty. Change-Id: I9c762b1fa04ea6bf9c0fba9e2459a20430c71c90 --- media/java/android/media/ExifInterface.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/media/java/android/media/ExifInterface.java b/media/java/android/media/ExifInterface.java index 6bf5721d55c30..445ee6f91508c 100644 --- a/media/java/android/media/ExifInterface.java +++ b/media/java/android/media/ExifInterface.java @@ -17,6 +17,8 @@ package android.media; import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; @@ -128,6 +130,9 @@ public class ExifInterface { // use sLock to serialize the accesses. private static final Object sLock = new Object(); + // Pattern to check non zero timestamp + private static final Pattern sNonZeroTimePattern = Pattern.compile(".*[1-9].*"); + /** * Reads Exif tags from the specified JPEG file. */ @@ -367,7 +372,8 @@ public class ExifInterface { */ public long getDateTime() { String dateTimeString = mAttributes.get(TAG_DATETIME); - if (dateTimeString == null) return -1; + if (dateTimeString == null + || !sNonZeroTimePattern.matcher(dateTimeString).matches()) return -1; ParsePosition pos = new ParsePosition(0); try { @@ -402,7 +408,9 @@ public class ExifInterface { public long getGpsDateTime() { String date = mAttributes.get(TAG_GPS_DATESTAMP); String time = mAttributes.get(TAG_GPS_TIMESTAMP); - if (date == null || time == null) return -1; + if (date == null || time == null + || (!sNonZeroTimePattern.matcher(date).matches() + && !sNonZeroTimePattern.matcher(time).matches())) return -1; String dateTimeString = date + ' ' + time;