Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2995493d authored by Jorge Ruesga's avatar Jorge Ruesga Committed by Steve Kondik
Browse files

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: default avatarJorge Ruesga <jorge@ruesga.com>

Change-Id: Ie3fdb34de83f4a863c0d1d23bb8162b2425955e9
parent 55a1c3e0
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -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;
    }
}
+6 −4
Original line number Diff line number Diff line
@@ -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;