cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [3/7] git commit: CB-1826 Catch OOM on gallery image resize
Date Fri, 28 Feb 2014 21:33:31 GMT
CB-1826 Catch OOM on gallery image resize


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/commit/4ae44148
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/tree/4ae44148
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/diff/4ae44148

Branch: refs/heads/master
Commit: 4ae4414856762e9250f5579c7052315d62733032
Parents: 61ba9cc
Author: Clément Vollet <clement@joshfire.com>
Authored: Tue Jan 28 12:11:57 2014 +0100
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Thu Feb 13 13:26:48 2014 -0500

----------------------------------------------------------------------
 src/android/CameraLauncher.java | 48 ++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera/blob/4ae44148/src/android/CameraLauncher.java
----------------------------------------------------------------------
diff --git a/src/android/CameraLauncher.java b/src/android/CameraLauncher.java
index 4e41d4b..d4ba168 100755
--- a/src/android/CameraLauncher.java
+++ b/src/android/CameraLauncher.java
@@ -84,6 +84,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
     private int mediaType;                  // What type of media to retrieve
     private boolean saveToPhotoAlbum;       // Should the picture be saved to the device's
photo album
     private boolean correctOrientation;     // Should the pictures orientation be corrected
+    private boolean orientationCorrected;   // Has the picture's orientation been corrected
     //private boolean allowEdit;              // Should we allow the user to crop the image.
UNUSED.
 
     public CallbackContext callbackContext;
@@ -364,9 +365,14 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
         bitmap = null;
     }
     
-    private String ouputResizedBitmap(Bitmap bitmap, Uri uri) throws IOException {
+    private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
         // Create an ExifHelper to save the exif data that is lost during compression
-        String resizePath = getTempDirectoryPath() + "/resize.jpg";
+        String modifiedPath = getTempDirectoryPath() + "/modified.jpg";
+
+        OutputStream os = new FileOutputStream(modifiedPath);
+        bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
+        os.close();
+
         // Some content: URIs do not map to file paths (e.g. picasa).
         String realPath = FileHelper.getRealPath(uri, this.cordova);
         ExifHelper exif = new ExifHelper();
@@ -374,22 +380,16 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
             try {
                 exif.createInFile(realPath);
                 exif.readExifData();
-                rotate = exif.getOrientation();
+                if (this.correctOrientation && this.orientationCorrected) {
+                    exif.resetOrientation();
+                }
+                exif.createOutFile(modifiedPath);
+                exif.writeExifData();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
-
-        OutputStream os = new FileOutputStream(resizePath);
-        bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
-        os.close();
-
-        // Restore exif data to file
-        if (realPath != null && this.encodingType == JPEG) {
-            exif.createOutFile(resizePath);
-            exif.writeExifData();
-        }
-        return resizePath;
+        return modifiedPath;
     }
 
     /**
@@ -440,7 +440,12 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
                     if (rotate != 0) {
                         Matrix matrix = new Matrix();
                         matrix.setRotate(rotate);
-                        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
matrix, true);
+                        try {
+                            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
+                            this.orientationCorrected = true;
+                        } catch (OutOfMemoryError oom) {
+                            this.orientationCorrected = false;
+                        }
                     }
                 }
 
@@ -451,13 +456,14 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
 
                 // If sending filename back
                 else if (destType == FILE_URI || destType == NATIVE_URI) {
-                    // Do we need to scale the returned file
-                    if (this.targetHeight > 0 && this.targetWidth > 0) {
+                    // Did we modify the image?
+                    if ( (this.targetHeight > 0 && this.targetWidth > 0) ||
+                            (this.correctOrientation && this.orientationCorrected)
) {
                         try {
-                            String resizePath = this.ouputResizedBitmap(bitmap, uri);
-                            // The resized image is cached by the app in order to get around
this and not have to delete you
+                            String modifiedPath = this.ouputModifiedBitmap(bitmap, uri);
+                            // The modified image is cached by the app in order to get around
this and not have to delete you
                             // application cache I'm adding the current system time to the
end of the file url.
-                            this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
+                            this.callbackContext.success("file://" + modifiedPath + "?" +
System.currentTimeMillis());
                         } catch (Exception e) {
                             e.printStackTrace();
                             this.failPicture("Error retrieving image.");
@@ -516,7 +522,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
         // If retrieving photo from library
         else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
             if (resultCode == Activity.RESULT_OK) {
-                this.processResultFromGallery(destType, intent)
+                this.processResultFromGallery(destType, intent);
             }
             else if (resultCode == Activity.RESULT_CANCELED) {
                 this.failPicture("Selection cancelled.");


Mime
View raw message