diff --git a/app/src/org/commcare/activities/components/ImageCaptureProcessing.java b/app/src/org/commcare/activities/components/ImageCaptureProcessing.java index 003c456c91..6f012311cf 100644 --- a/app/src/org/commcare/activities/components/ImageCaptureProcessing.java +++ b/app/src/org/commcare/activities/components/ImageCaptureProcessing.java @@ -56,7 +56,8 @@ private static Pair moveAndScaleImage(File originalImage, boolean if (currentWidget != null) { int maxDimen = currentWidget.getMaxDimen(); if (maxDimen != -1) { - savedScaledImage = FileUtil.scaleAndSaveImage(originalImage, tempFilePathForScaledImage, maxDimen); + File tempFile = new File(tempFilePathForScaledImage); + savedScaledImage = FileUtil.scaleAndSaveImageWithExif(originalImage, tempFile, maxDimen); } } } @@ -92,7 +93,7 @@ private static File makeRawCopy(File originalImage, String instanceFolder, Strin } File rawImageFile = new File(rawDirPath + "/" + imageFilename); try { - FileUtil.copyFile(originalImage, rawImageFile); + FileUtil.copyFileWithExifData(originalImage, rawImageFile); } catch (Exception e) { throw new IOException("Failed to rename " + originalImage.getAbsolutePath() + " to " + rawImageFile.getAbsolutePath()); diff --git a/app/src/org/commcare/utils/FileUtil.java b/app/src/org/commcare/utils/FileUtil.java index 002bba8b99..e26b9c90ce 100644 --- a/app/src/org/commcare/utils/FileUtil.java +++ b/app/src/org/commcare/utils/FileUtil.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; +import android.media.ExifInterface; import android.media.MediaMetadataRetriever; import android.net.Uri; import android.os.Build; @@ -857,4 +858,72 @@ public static boolean isFileTooLargeToUpload(ContentResolver contentResolver, Ur return returnCursor.getLong(sizeIndex) > FormUploadUtil.MAX_BYTES; } } + + public static void copyFileWithExifData(File sourceFile, File destFile) throws IOException { + // First copy the file normally + copyFile(sourceFile, destFile); + + // Then copy EXIF data + ExifInterface sourceExif = new ExifInterface(sourceFile.getAbsolutePath()); + ExifInterface destExif = new ExifInterface(destFile.getAbsolutePath()); + + // Copy GPS data + String[] tagsToPreserve = { + ExifInterface.TAG_GPS_LATITUDE, + ExifInterface.TAG_GPS_LATITUDE_REF, + ExifInterface.TAG_GPS_LONGITUDE, + ExifInterface.TAG_GPS_LONGITUDE_REF, + ExifInterface.TAG_GPS_TIMESTAMP, + ExifInterface.TAG_GPS_DATESTAMP, + // Preserve other important EXIF data + ExifInterface.TAG_DATETIME, + ExifInterface.TAG_ORIENTATION + }; + + for (String tag : tagsToPreserve) { + String value = sourceExif.getAttribute(tag); + if (value != null) { + destExif.setAttribute(tag, value); + } + } + + destExif.saveAttributes(); + } + + public static boolean scaleAndSaveImageWithExif(File sourceFile, File destFile, int maxDimen) throws IOException { + // First scale the image + boolean scaled = scaleAndSaveImage(sourceFile, destFile.getAbsolutePath(), maxDimen); + + if (scaled) { + // Copy EXIF data from source to scaled image + ExifInterface sourceExif = new ExifInterface(sourceFile.getAbsolutePath()); + ExifInterface destExif = new ExifInterface(destFile.getAbsolutePath()); + + // Copy GPS and other important EXIF data + copyExifData(sourceExif, destExif); + destExif.saveAttributes(); + } + + return scaled; + } + + private static void copyExifData(ExifInterface source, ExifInterface dest) { + String[] tagsToPreserve = { + ExifInterface.TAG_GPS_LATITUDE, + ExifInterface.TAG_GPS_LATITUDE_REF, + ExifInterface.TAG_GPS_LONGITUDE, + ExifInterface.TAG_GPS_LONGITUDE_REF, + ExifInterface.TAG_GPS_TIMESTAMP, + ExifInterface.TAG_GPS_DATESTAMP, + ExifInterface.TAG_DATETIME, + ExifInterface.TAG_ORIENTATION + }; + + for (String tag : tagsToPreserve) { + String value = source.getAttribute(tag); + if (value != null) { + dest.setAttribute(tag, value); + } + } + } }