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

Commit ebe9c749 authored by Paresh Nakhe's avatar Paresh Nakhe Committed by Gerrit - the friendly Code Review server
Browse files

wallpapercropper: OOM issue while opening *.bmp image

This change manages setting of large images as
wallpapers without encountering OOM.

CRs-fixed: 619815
Change-Id: I3a32ec4b42050b146076f0adf6b6e65afd6fae4e
parent 4ce17650
Loading
Loading
Loading
Loading
+33 −4
Original line number Diff line number Diff line
@@ -83,6 +83,12 @@ public class WallpaperCropActivity extends Activity {
        }
    }

   @Override
   protected void onDestroy(){
        super.onDestroy();
        mCropView.destroy();
    }

    protected void init() {
        setContentView(R.layout.wallpaper_cropper);

@@ -506,7 +512,13 @@ public class WallpaperCropActivity extends Activity {
            InputStream is = regenerateInputStream();
            if (is != null) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                //inSampleSize - decreases height/width of image to be loaded by a factor of 4
                //inPurgeable - pixels can be purged if system needs to reclaim memory
                //inInputShareable - bitmap keeps a shallow reference to input
                options.inJustDecodeBounds = true;
                options.inSampleSize = 4;
                options.inInputShareable = true;
                options.inPurgeable = true;
                BitmapFactory.decodeStream(is, null, options);
                Utils.closeSilently(is);
                if (options.outWidth != 0 && options.outHeight != 0) {
@@ -584,7 +596,9 @@ public class WallpaperCropActivity extends Activity {
                }

                // See how much we're reducing the size of the image
                int scaleDownSampleSize = Math.max(1, Math.min(roundedTrueCrop.width() / mOutWidth,
                //This is a hard coded optimization
                //No apparent degradation in quality but major reduction in amt of memory consumed.
                int scaleDownSampleSize = Math.max(4, Math.min(roundedTrueCrop.width() / mOutWidth,
                        roundedTrueCrop.height() / mOutHeight));
                // Attempt to open a region decoder
                BitmapRegionDecoder decoder = null;
@@ -609,6 +623,8 @@ public class WallpaperCropActivity extends Activity {
                if (decoder != null) {
                    // Do region decoding to get crop bitmap
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inPurgeable = true;
                    options.inInputShareable = true;
                    if (scaleDownSampleSize > 1) {
                        options.inSampleSize = scaleDownSampleSize;
                    }
@@ -620,8 +636,11 @@ public class WallpaperCropActivity extends Activity {
                    // BitmapRegionDecoder has failed, try to crop in-memory
                    is = regenerateInputStream();
                    Bitmap fullSize = null;

                    if (is != null) {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inPurgeable = true;
                        options.inInputShareable = true;
                        if (scaleDownSampleSize > 1) {
                            options.inSampleSize = scaleDownSampleSize;
                        }
@@ -643,9 +662,10 @@ public class WallpaperCropActivity extends Activity {
                                    roundedTrueCrop.right, (int) mCropBounds.bottom);
                        }

                        crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
                                roundedTrueCrop.top, roundedTrueCrop.width(),
                                roundedTrueCrop.height());
                        crop = Bitmap.createScaledBitmap(fullSize, roundedTrueCrop.width(),
                                 roundedTrueCrop.height(), false);
                        fullSize.recycle();
                        fullSize = null;
                    }
                }

@@ -695,6 +715,10 @@ public class WallpaperCropActivity extends Activity {
                        Paint p = new Paint();
                        p.setFilterBitmap(true);
                        c.drawBitmap(crop, m, p);
                        if (crop != null) {
                            crop.recycle();
                            crop = null;
                        }
                        crop = tmp;
                    }
                }
@@ -727,6 +751,11 @@ public class WallpaperCropActivity extends Activity {
                    Log.w(LOGTAG, "cannot compress bitmap");
                    failure = true;
                }
                try {
                    tmpOut.close();
                } catch (IOException e) {
                    Log.w(LOGTAG, "Error closing ByteArray");
                }
            }
            return !failure; // True if any of the operations failed
        }