diff options
author | Angus Kong <shkong@google.com> | 2012-12-19 04:16:29 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-12-19 04:16:30 -0800 |
commit | 9ac82ea62edc0320bab52dd37294d2b52c219961 (patch) | |
tree | 39a05c956f7c91b5971acca32f43b356407cf7ab /src/com/android/gallery3d/data | |
parent | d81e17b5c783e5eb04c09151c8fd7f3f3aa487e9 (diff) | |
parent | dcb9deb9654ba141bfbf9788eab2bc3c83a8f24f (diff) | |
download | android_packages_apps_Gallery2-9ac82ea62edc0320bab52dd37294d2b52c219961.tar.gz android_packages_apps_Gallery2-9ac82ea62edc0320bab52dd37294d2b52c219961.tar.bz2 android_packages_apps_Gallery2-9ac82ea62edc0320bab52dd37294d2b52c219961.zip |
Merge "Apply ExifModifier for rotation" into gb-ub-photos-bryce
Diffstat (limited to 'src/com/android/gallery3d/data')
-rw-r--r-- | src/com/android/gallery3d/data/LocalImage.java | 49 | ||||
-rw-r--r-- | src/com/android/gallery3d/data/MediaDetails.java | 2 |
2 files changed, 36 insertions, 15 deletions
diff --git a/src/com/android/gallery3d/data/LocalImage.java b/src/com/android/gallery3d/data/LocalImage.java index b2be1246a..d5fad5483 100644 --- a/src/com/android/gallery3d/data/LocalImage.java +++ b/src/com/android/gallery3d/data/LocalImage.java @@ -36,6 +36,10 @@ import com.android.gallery3d.app.PanoramaMetadataSupport; import com.android.gallery3d.app.StitchingProgressManager; import com.android.gallery3d.common.ApiHelper; import com.android.gallery3d.common.BitmapUtils; +import com.android.gallery3d.common.Utils; +import com.android.gallery3d.exif.ExifInvalidFormatException; +import com.android.gallery3d.exif.ExifModifier; +import com.android.gallery3d.exif.ExifTag; import com.android.gallery3d.util.GalleryUtils; import com.android.gallery3d.util.ThreadPool.Job; import com.android.gallery3d.util.ThreadPool.JobContext; @@ -43,6 +47,8 @@ import com.android.gallery3d.util.UpdateHelper; import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel.MapMode; // LocalImage represents an image in the local storage. public class LocalImage extends LocalMediaItem { @@ -270,16 +276,16 @@ public class LocalImage extends LocalMediaItem { new String[]{String.valueOf(id)}); } - private static String getExifOrientation(int orientation) { + private static int getExifOrientation(int orientation) { switch (orientation) { case 0: - return String.valueOf(ExifInterface.ORIENTATION_NORMAL); + return ExifInterface.ORIENTATION_NORMAL; case 90: - return String.valueOf(ExifInterface.ORIENTATION_ROTATE_90); + return ExifInterface.ORIENTATION_ROTATE_90; case 180: - return String.valueOf(ExifInterface.ORIENTATION_ROTATE_180); + return ExifInterface.ORIENTATION_ROTATE_180; case 270: - return String.valueOf(ExifInterface.ORIENTATION_ROTATE_270); + return ExifInterface.ORIENTATION_ROTATE_270; default: throw new AssertionError("invalid: " + orientation); } @@ -294,18 +300,35 @@ public class LocalImage extends LocalMediaItem { if (rotation < 0) rotation += 360; if (mimeType.equalsIgnoreCase("image/jpeg")) { + RandomAccessFile file = null; try { - ExifInterface exif = new ExifInterface(filePath); - exif.setAttribute(ExifInterface.TAG_ORIENTATION, - getExifOrientation(rotation)); - exif.saveAttributes(); + // Because most of the images contain the orientation tag, we + // use ExifModifier to modify the tag for better efficiency. + // If the tag doesn't exist, ExifInterface will be used to replace the entire + // header. + file = new RandomAccessFile(filePath, "rw"); + ExifModifier modifier = new ExifModifier( + file.getChannel().map(MapMode.READ_WRITE, 0, file.length())); + ExifTag tag = ExifTag.buildTag(ExifTag.TAG_ORIENTATION); + tag.setValue(getExifOrientation(rotation)); + modifier.modifyTag(tag); + if (!modifier.commit()) { + // Need to change the file size, use ExifInterface instead. + ExifInterface exif = new ExifInterface(filePath); + exif.setAttribute(ExifInterface.TAG_ORIENTATION, + String.valueOf(getExifOrientation(rotation))); + exif.saveAttributes(); + // We need to update the filesize as well + fileSize = new File(filePath).length(); + values.put(Images.Media.SIZE, fileSize); + } } catch (IOException e) { Log.w(TAG, "cannot set exif data: " + filePath); + } catch (ExifInvalidFormatException e) { + Log.w(TAG, "cannot set exif data: " + filePath); + } finally { + Utils.closeSilently(file); } - - // We need to update the filesize as well - fileSize = new File(filePath).length(); - values.put(Images.Media.SIZE, fileSize); } values.put(Images.Media.ORIENTATION, rotation); diff --git a/src/com/android/gallery3d/data/MediaDetails.java b/src/com/android/gallery3d/data/MediaDetails.java index 16716dae4..662bd141c 100644 --- a/src/com/android/gallery3d/data/MediaDetails.java +++ b/src/com/android/gallery3d/data/MediaDetails.java @@ -16,8 +16,6 @@ package com.android.gallery3d.data; -import android.media.ExifInterface; - import com.android.gallery3d.R; import com.android.gallery3d.common.Utils; import com.android.gallery3d.exif.ExifData; |