diff options
author | Ruben Brunk <rubenbrunk@google.com> | 2013-02-20 22:45:55 -0800 |
---|---|---|
committer | Ruben Brunk <rubenbrunk@google.com> | 2013-02-21 12:55:46 -0800 |
commit | 182659f7e3b04af88198cc3004af96e4944e32e5 (patch) | |
tree | 71a572ec4da66e61bc629493cc247617fd9d1537 /src/com/android/gallery3d/filtershow/tools/SaveCopyTask.java | |
parent | aa4c34606a697172987e5ac379e82ce41b7cbaa5 (diff) | |
download | android_packages_apps_Gallery2-182659f7e3b04af88198cc3004af96e4944e32e5.tar.gz android_packages_apps_Gallery2-182659f7e3b04af88198cc3004af96e4944e32e5.tar.bz2 android_packages_apps_Gallery2-182659f7e3b04af88198cc3004af96e4944e32e5.zip |
Temporary handling when running out of heap in filtershow.
Bug: 8233895
Change-Id: Id078d2a4b387127c0d230bc5d9de4590f0e9f72b
Diffstat (limited to 'src/com/android/gallery3d/filtershow/tools/SaveCopyTask.java')
-rw-r--r-- | src/com/android/gallery3d/filtershow/tools/SaveCopyTask.java | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/src/com/android/gallery3d/filtershow/tools/SaveCopyTask.java b/src/com/android/gallery3d/filtershow/tools/SaveCopyTask.java index e378fe2b7..89cfa6bdf 100644 --- a/src/com/android/gallery3d/filtershow/tools/SaveCopyTask.java +++ b/src/com/android/gallery3d/filtershow/tools/SaveCopyTask.java @@ -22,6 +22,7 @@ import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; +import android.graphics.BitmapFactory; import android.net.Uri; import android.os.AsyncTask; import android.os.Environment; @@ -173,37 +174,52 @@ public class SaveCopyTask extends AsyncTask<ImagePreset, Void, Uri> { } ImagePreset preset = params[0]; InputStream is = null; - try { - Bitmap bitmap = ImageLoader.loadMutableBitmap(context, sourceUri); - if (bitmap == null) { - return null; - } - bitmap = preset.applyGeometry(bitmap); - bitmap = preset.apply(bitmap); + BitmapFactory.Options options = new BitmapFactory.Options(); + boolean noBitmap = true; + int num_tries = 0; + // Stopgap fix for low-memory devices. + while(noBitmap) { + try { + // Try to do bitmap operations, downsample if low-memory + Bitmap bitmap = ImageLoader.loadMutableBitmap(context, sourceUri, options); + if (bitmap == null) { + return null; + } + bitmap = preset.applyGeometry(bitmap); + bitmap = preset.apply(bitmap); - Object xmp = null; - if (preset.isPanoramaSafe()) { - is = context.getContentResolver().openInputStream(sourceUri); - xmp = XmpUtilHelper.extractXMPMeta(is); - } - ExifData exif = getExifData(sourceUri); - if (exif != null) { - exif.addDateTimeStampTag(ExifTag.TAG_DATE_TIME, System.currentTimeMillis(), - TimeZone.getDefault()); - // Since the image has been modified, set the orientation to normal. - exif.addTag(ExifTag.TAG_ORIENTATION).setValue(ExifTag.Orientation.TOP_LEFT); + Object xmp = null; + if (preset.isPanoramaSafe()) { + is = context.getContentResolver().openInputStream(sourceUri); + xmp = XmpUtilHelper.extractXMPMeta(is); + } + ExifData exif = getExifData(sourceUri); + if (exif != null) { + exif.addDateTimeStampTag(ExifTag.TAG_DATE_TIME, System.currentTimeMillis(), + TimeZone.getDefault()); + // Since the image has been modified, set the orientation to normal. + exif.addTag(ExifTag.TAG_ORIENTATION).setValue(ExifTag.Orientation.TOP_LEFT); + } + saveBitmap(bitmap, this.destinationFile, xmp, exif); + bitmap.recycle(); + noBitmap = false; + } catch (FileNotFoundException ex) { + Log.w(LOGTAG, "Failed to save image!", ex); + return null; + } catch (java.lang.OutOfMemoryError e) { + // Try 5 times before failing for good. + if (++num_tries >= 5) { + throw e; + } + System.gc(); + options.inSampleSize *= 2; + } finally { + Utils.closeSilently(is); } - saveBitmap(bitmap, this.destinationFile, xmp, exif); - - Uri uri = insertContent(context, sourceUri, this.destinationFile, saveFileName); - bitmap.recycle(); - return uri; - } catch (FileNotFoundException ex) { - Log.w(LOGTAG, "Failed to save image!", ex); - return null; - } finally { - Utils.closeSilently(is); } + Uri uri = insertContent(context, sourceUri, this.destinationFile, saveFileName); + return uri; + } @Override |