You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ma...@apache.org on 2012/07/06 18:37:36 UTC

android commit: CB-1014: Out of Memory error when getting image from photo library

Updated Branches:
  refs/heads/master 1f46240ba -> eb0348d47


CB-1014: Out of Memory error when getting image from photo library


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/eb0348d4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/eb0348d4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/eb0348d4

Branch: refs/heads/master
Commit: eb0348d47c9f3d2be7230f37ea174df381a82b91
Parents: 1f46240
Author: macdonst <si...@gmail.com>
Authored: Fri Jul 6 12:37:08 2012 -0400
Committer: macdonst <si...@gmail.com>
Committed: Fri Jul 6 12:37:08 2012 -0400

----------------------------------------------------------------------
 .../src/org/apache/cordova/CameraLauncher.java     |  122 ++++++++-------
 1 files changed, 64 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/eb0348d4/framework/src/org/apache/cordova/CameraLauncher.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CameraLauncher.java b/framework/src/org/apache/cordova/CameraLauncher.java
index fa19ca3..a85c97b 100755
--- a/framework/src/org/apache/cordova/CameraLauncher.java
+++ b/framework/src/org/apache/cordova/CameraLauncher.java
@@ -370,75 +370,81 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
                     this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
                 }
                 else {
-
-                    // Get the path to the image. Makes loading so much easier.
-                    String imagePath = FileUtils.getRealPathFromURI(uri, this.cordova);
-                    Bitmap bitmap = getScaledBitmap(imagePath);
-
-                    if (this.correctOrientation) {
-                        String[] cols = { MediaStore.Images.Media.ORIENTATION };
-                        Cursor cursor = this.cordova.getActivity().getContentResolver().query(intent.getData(),
-                                cols, null, null, null);
-                        if (cursor != null) {
-                            cursor.moveToPosition(0);
-                            rotate = cursor.getInt(0);
-                            cursor.close();
-                        }
-                        if (rotate != 0) {
-                            Matrix matrix = new Matrix();
-                            matrix.setRotate(rotate);
-                            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
+                    // This is a special case to just return the path as no scaling,
+                    // rotating or compression needs to be done
+                    if (this.targetHeight == -1 && this.targetWidth == -1 &&
+                            this.mQuality == 100 && destType == FILE_URI && !this.correctOrientation) {
+                        this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
+                    } else {
+                        // Get the path to the image. Makes loading so much easier.
+                        String imagePath = FileUtils.getRealPathFromURI(uri, this.cordova);
+                        Bitmap bitmap = getScaledBitmap(imagePath);
+
+                        if (this.correctOrientation) {
+                            String[] cols = { MediaStore.Images.Media.ORIENTATION };
+                            Cursor cursor = this.cordova.getActivity().getContentResolver().query(intent.getData(),
+                                    cols, null, null, null);
+                            if (cursor != null) {
+                                cursor.moveToPosition(0);
+                                rotate = cursor.getInt(0);
+                                cursor.close();
+                            }
+                            if (rotate != 0) {
+                                Matrix matrix = new Matrix();
+                                matrix.setRotate(rotate);
+                                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
+                            }
                         }
-                    }
 
-                    // If sending base64 image back
-                    if (destType == DATA_URL) {
-                        this.processPicture(bitmap);
-                    }
+                        // If sending base64 image back
+                        if (destType == DATA_URL) {
+                            this.processPicture(bitmap);
+                        }
 
-                    // If sending filename back
-                    else if (destType == FILE_URI) {
-                        // Do we need to scale the returned file
-                        if (this.targetHeight > 0 && this.targetWidth > 0) {
-                            try {
-                                // Create an ExifHelper to save the exif data that is lost during compression
-                                String resizePath = DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/resize.jpg";
-                                ExifHelper exif = new ExifHelper();
+                        // If sending filename back
+                        else if (destType == FILE_URI) {
+                            // Do we need to scale the returned file
+                            if (this.targetHeight > 0 && this.targetWidth > 0) {
                                 try {
-                                    if (this.encodingType == JPEG) {
-                                        exif.createInFile(resizePath);
-                                        exif.readExifData();
-                                        rotate = exif.getOrientation();
+                                    // Create an ExifHelper to save the exif data that is lost during compression
+                                    String resizePath = DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/resize.jpg";
+                                    ExifHelper exif = new ExifHelper();
+                                    try {
+                                        if (this.encodingType == JPEG) {
+                                            exif.createInFile(resizePath);
+                                            exif.readExifData();
+                                            rotate = exif.getOrientation();
+                                        }
+                                    } catch (IOException e) {
+                                        e.printStackTrace();
                                     }
-                                } catch (IOException e) {
-                                    e.printStackTrace();
-                                }
 
-                                OutputStream os = new FileOutputStream(resizePath);
-                                bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
-                                os.close();
+                                    OutputStream os = new FileOutputStream(resizePath);
+                                    bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
+                                    os.close();
 
-                                // Restore exif data to file
-                                if (this.encodingType == JPEG) {
-                                    exif.createOutFile(FileUtils.getRealPathFromURI(uri, this.cordova));
-                                    exif.writeExifData();
-                                }
+                                    // Restore exif data to file
+                                    if (this.encodingType == JPEG) {
+                                        exif.createOutFile(FileUtils.getRealPathFromURI(uri, this.cordova));
+                                        exif.writeExifData();
+                                    }
 
-                                // The resized 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.success(new PluginResult(PluginResult.Status.OK, ("file://" + resizePath + "?" + System.currentTimeMillis())), this.callbackId);
-                            } catch (Exception e) {
-                                e.printStackTrace();
-                                this.failPicture("Error retrieving image.");
+                                    // The resized 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.success(new PluginResult(PluginResult.Status.OK, ("file://" + resizePath + "?" + System.currentTimeMillis())), this.callbackId);
+                                } catch (Exception e) {
+                                    e.printStackTrace();
+                                    this.failPicture("Error retrieving image.");
+                                }
+                            }
+                            else {
+                                this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
                             }
                         }
-                        else {
-                            this.success(new PluginResult(PluginResult.Status.OK, uri.toString()), this.callbackId);
-                        }
+                        bitmap.recycle();
+                        bitmap = null;
+                        System.gc();
                     }
-                    bitmap.recycle();
-                    bitmap = null;
-                    System.gc();
                 }
             }
             else if (resultCode == Activity.RESULT_CANCELED) {