Merge "FP2-1171:Unfortunately,Gallery has stopped' pops up when set a big picture as contact's photo" into fp2-dev-v2
diff --git a/src/com/android/gallery3d/filtershow/crop/CropActivity.java b/src/com/android/gallery3d/filtershow/crop/CropActivity.java
index 3a78296..6dacd01 100644
--- a/src/com/android/gallery3d/filtershow/crop/CropActivity.java
+++ b/src/com/android/gallery3d/filtershow/crop/CropActivity.java
@@ -490,7 +490,25 @@
// Do region decoding to get crop bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
- crop = decoder.decodeRegion(roundedTrueCrop, options);
+ boolean loop = true;
+ int simpleSize = 1;
+ int times = 0;
+ while (loop) {
+ try {
+ options.inSampleSize = simpleSize;
+ crop = decoder.decodeRegion(roundedTrueCrop, options);
+ loop = false;
+ } catch (OutOfMemoryError e) {
+ // try 5 times before failing
+ if (++times >= 5)
+ loop = false;
+ System.gc();
+ simpleSize *= 2;
+ } catch (Exception e) {
+ loop = false;
+ Log.e(LOGTAG, " exception when crop picture: " + e);
+ }
+ }
decoder.recycle();
}
@@ -499,12 +517,16 @@
regenerateInputStream();
Bitmap fullSize = null;
if (mInStream != null) {
- fullSize = BitmapFactory.decodeStream(mInStream);
- }
- if (fullSize != null) {
- crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
- roundedTrueCrop.top, roundedTrueCrop.width(),
- roundedTrueCrop.height());
+ try {
+ fullSize = BitmapFactory.decodeStream(mInStream);
+ } catch (OutOfMemoryError e) {
+ Log.d(LOGTAG, "OOM when decode Bitmap", e);
+ }
+ if (fullSize != null) {
+ crop = createBitmapWithFixedSize(fullSize, roundedTrueCrop.left,
+ roundedTrueCrop.top, roundedTrueCrop.width(),
+ roundedTrueCrop.height());
+ }
}
}
@@ -695,4 +717,25 @@
RectF scaledCrop = CropMath.getScaledCropBounds(crop, photo, imageBounds);
return scaledCrop;
}
+
+ private static Bitmap createBitmapWithFixedSize(Bitmap source, int x, int y, int width, int height) {
+ boolean loop = true;
+ int ratio = 1;
+ Bitmap bitmap = null;
+ while (loop) {
+ try {
+ bitmap = Bitmap.createBitmap(source, x, y, width / ratio, height / ratio);
+ loop = false;
+ } catch (OutOfMemoryError e) {
+ if (ratio > 4)
+ loop = false;
+ System.gc();
+ ratio *= 2;
+ } catch (Exception e) {
+ loop = false;
+ Log.e(LOGTAG, "exception when crop picture : " + e);
+ }
+ }
+ return bitmap;
+ }
}