summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJorge Ruesga <jorge@ruesga.com>2014-09-02 03:18:42 +0200
committerSteve Kondik <steve@cyngn.com>2015-03-28 14:58:54 -0700
commit2995493d0f3843f84b606c09d0a6a9a89ae6a3f0 (patch)
tree65d9c5f5cb6cba3071f7b7089f368d5603cb35d5 /src
parent55a1c3e023853cf5169d5a5f049ee566146ddf2b (diff)
downloadandroid_packages_apps_Gallery2-2995493d0f3843f84b606c09d0a6a9a89ae6a3f0.zip
android_packages_apps_Gallery2-2995493d0f3843f84b606c09d0a6a9a89ae6a3f0.tar.gz
android_packages_apps_Gallery2-2995493d0f3843f84b606c09d0a6a9a89ae6a3f0.tar.bz2
gallery: use inJustDecodeBounds in ImageFilterBorder
To avoid OOM exceptions loading the border drawable JIRA: BUGDUMP-20716 https://jira.cyanogenmod.org/browse/BUGDUMP-20716 Signed-off-by: Jorge Ruesga <jorge@ruesga.com> Change-Id: Ie3fdb34de83f4a863c0d1d23bb8162b2425955e9
Diffstat (limited to 'src')
-rw-r--r--src/com/android/gallery3d/data/DecodeUtils.java44
-rw-r--r--src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java10
2 files changed, 50 insertions, 4 deletions
diff --git a/src/com/android/gallery3d/data/DecodeUtils.java b/src/com/android/gallery3d/data/DecodeUtils.java
index b92e877..1240518 100644
--- a/src/com/android/gallery3d/data/DecodeUtils.java
+++ b/src/com/android/gallery3d/data/DecodeUtils.java
@@ -17,6 +17,7 @@
package com.android.gallery3d.data;
import android.annotation.TargetApi;
+import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
@@ -33,6 +34,7 @@ import com.android.gallery3d.ui.Log;
import com.android.gallery3d.util.ThreadPool.CancelListener;
import com.android.gallery3d.util.ThreadPool.JobContext;
+import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.InputStream;
@@ -317,4 +319,46 @@ public class DecodeUtils {
decodeBounds(jc, fileDescriptor, options);
return GalleryBitmapPool.getInstance().get(options.outWidth, options.outHeight);
}
+
+ public static Bitmap decodeBitmap(Resources res, int resId, int reqWidth, int reqHeight) {
+ // First decode with inJustDecodeBounds=true to check dimensions
+ final BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeResource(res, resId, options);
+
+ // Calculate inSampleSize (use 1024 as maximum size, the minimum supported
+ // by all the gles20 devices)
+ options.inSampleSize = calculateBitmapRatio(
+ options,
+ Math.min(reqWidth, 1024),
+ Math.min(reqHeight, 1024));
+
+ // Decode the bitmap with inSampleSize set
+ options.inJustDecodeBounds = false;
+ options.inPreferQualityOverSpeed = false;
+ options.inPurgeable = true;
+ options.inInputShareable = true;
+ options.inDither = true;
+ return BitmapFactory.decodeResource(res, resId, options);
+ }
+
+ private static int calculateBitmapRatio(Options options, int reqWidth, int reqHeight) {
+ // Raw height and width of image
+ final int height = options.outHeight;
+ final int width = options.outWidth;
+ int inSampleSize = 1;
+
+ if (height > reqHeight || width > reqWidth) {
+ // Calculate ratios of height and width to requested height and width
+ final int heightRatio = Math.round((float) height / (float) reqHeight);
+ final int widthRatio = Math.round((float) width / (float) reqWidth);
+
+ // Choose the smallest ratio as inSampleSize value, this will guarantee
+ // a final image with both dimensions larger than or equal to the
+ // requested height and width.
+ inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
+ }
+
+ return inSampleSize;
+ }
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java
index 0f9e7b9..5f35022 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java
@@ -18,12 +18,13 @@ package com.android.gallery3d.filtershow.filters;
import android.content.res.Resources;
import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
+import com.android.gallery3d.data.DecodeUtils;
+
import java.util.HashMap;
import java.lang.ref.WeakReference;
@@ -58,7 +59,7 @@ public class ImageFilterBorder extends ImageFilter {
Rect bounds = new Rect(0, 0, (int) (w * scale1), (int) (h * scale1));
Canvas canvas = new Canvas(bitmap);
canvas.scale(scale2, scale2);
- Drawable drawable = getDrawable(getParameters().getDrawableResource());
+ Drawable drawable = getDrawable(getParameters().getDrawableResource(), w, h);
drawable.setBounds(bounds);
drawable.draw(canvas);
return bitmap;
@@ -81,10 +82,11 @@ public class ImageFilterBorder extends ImageFilter {
}
}
- public Drawable getDrawable(int rsc) {
+ public Drawable getDrawable(int rsc, int reqWidth, int reqHeight) {
Drawable drawable = (mDrawables.get(rsc) != null) ? mDrawables.get(rsc).get() : null;
if (drawable == null && mResources != null && rsc != 0) {
- drawable = new BitmapDrawable(mResources, BitmapFactory.decodeResource(mResources, rsc));
+ drawable = new BitmapDrawable(mResources, DecodeUtils.decodeBitmap(
+ mResources, rsc, reqWidth, reqHeight));
mDrawables.put(rsc, new WeakReference<Drawable>(drawable));
}
return drawable;