OSDN Git Service

Mirror CL 319301 in Bryce.
authorRuben Brunk <rubenbrunk@google.com>
Tue, 11 Jun 2013 20:21:02 +0000 (13:21 -0700)
committerRuben Brunk <rubenbrunk@google.com>
Tue, 11 Jun 2013 20:25:33 +0000 (13:25 -0700)
Change-Id: If84a08aa1969415a0d5d9ad3b9153743b1951108

src/com/android/gallery3d/filtershow/crop/CropLoader.java

index fc461f5..53a9ebc 100644 (file)
@@ -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;