From 1e2b3f8be10ca20f6807ed71f160d6f6a8ab1c81 Mon Sep 17 00:00:00 2001 From: Ruben Brunk Date: Tue, 11 Jun 2013 13:21:02 -0700 Subject: Mirror CL 319301 in Bryce. Change-Id: If84a08aa1969415a0d5d9ad3b9153743b1951108 --- .../gallery3d/filtershow/crop/CropLoader.java | 56 ++++++++++++++-------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/com/android/gallery3d/filtershow/crop/CropLoader.java b/src/com/android/gallery3d/filtershow/crop/CropLoader.java index fc461f5d0..53a9ebc14 100644 --- a/src/com/android/gallery3d/filtershow/crop/CropLoader.java +++ b/src/com/android/gallery3d/filtershow/crop/CropLoader.java @@ -30,6 +30,7 @@ import android.provider.MediaStore; import android.provider.MediaStore.Images; import android.provider.MediaStore.Images.ImageColumns; import android.util.Log; +import android.webkit.MimeTypeMap; import com.android.gallery3d.common.Utils; import com.android.gallery3d.exif.ExifInterface; @@ -64,39 +65,45 @@ public abstract class CropLoader { if (uri == null || context == null) { throw new IllegalArgumentException("bad argument to getScaledBitmap"); } - if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { - String mimeType = context.getContentResolver().getType(uri); - if (mimeType != JPEG_MIME_TYPE) { - return 0; - } - String path = uri.getPath(); - int orientation = 0; - ExifInterface exif = new ExifInterface(); - try { - exif.readExif(path); - orientation = ExifInterface.getRotationForOrientationValue( - exif.getTagIntValue(ExifInterface.TAG_ORIENTATION).shortValue()); - } catch (IOException e) { - Log.w(LOGTAG, "Failed to read EXIF orientation", e); - } - return orientation; - } + + // First try to find orientation data in Gallery's ContentProvider. Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); - if (cursor.moveToNext()) { + if (cursor != null && cursor.moveToNext()) { int ori = cursor.getInt(0); return (ori < 0) ? 0 : ori; } } catch (SQLiteException e) { - return 0; + // Do nothing } catch (IllegalArgumentException e) { - return 0; + // Do nothing } finally { Utils.closeSilently(cursor); } + + // Fall back to checking EXIF tags in file. + if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { + String mimeType = getMimeType(uri); + if (!JPEG_MIME_TYPE.equals(mimeType)) { + return 0; + } + String path = uri.getPath(); + int orientation = 0; + ExifInterface exif = new ExifInterface(); + try { + exif.readExif(path); + Integer tagval = exif.getTagIntValue(ExifInterface.TAG_ORIENTATION); + if (tagval != null) { + orientation = ExifInterface.getRotationForOrientationValue(tagval.shortValue()); + } + } catch (IOException e) { + Log.w(LOGTAG, "Failed to read EXIF orientation", e); + } + return orientation; + } return 0; } @@ -252,6 +259,15 @@ public abstract class CropLoader { return dir[0]; } + private static String getMimeType(Uri src) { + String postfix = MimeTypeMap.getFileExtensionFromUrl(src.toString()); + String ret = null; + if (postfix != null) { + ret = MimeTypeMap.getSingleton().getMimeTypeFromExtension(postfix); + } + return ret; + } + public static Uri insertContent(Context context, Uri sourceUri, File file, String saveFileName, long time) { time /= 1000; -- cgit v1.2.3