3

Background

Suppose I have a rather large bitmap, and I want to blur it into a smaller bitmap.

I actually ask this because I got weird crashes within Renderscript itself on some rare devices, so maybe it's something with the input (which in fact I already make sure is quite small):

java.lang.OutOfMemoryError: Failed to allocate a 1920169996 byte allocation with 16777216 free bytes and 67MB until OOM
   at android.renderscript.RenderScript$MessageThread.run(RenderScript.java:1111)

It's weird, because there is no way the input bitmap is so large (almost 2GB?!).

The problem

The code below tries to do it, but for some reason, the output bitmap takes only the top-left area of the large bitmap into itself:

        outputBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outputBitmap);
        canvas.drawBitmap(srcBitmap, 0, 0, null);
        Allocation overlayAlloc = Allocation.createFromBitmap(rs, outputBitmap, MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE);
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        blur.setInput(overlayAlloc);
        blur.setRadius(radius);
        blur.forEach(overlayAlloc);
        overlayAlloc.copyTo(outputBitmap);
        rs.destroy();
        return outputBitmap;

The question

How could this be? What should be done to fix it? Obviously, I could create a new bitmap as the input, and make it the same size as the small one, but this creates yet another bitmap that takes memory, and I'd like to avoid it.

android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

0

Before trying to save memory without creating small bitmaps try to check this: http://trickyandroid.com/advanced-blurring-techniques/

You can save time if you downscale you image, blur it and then upscale - there is no big difference between tricky blur and fair blur but it saves time.

PS. also you can check renderscript sample from this topic. PSS. also you can check this thread about blur: Fast Bitmap Blur For Android SDK

Community
  • 1
  • 1
dilix
  • 3,761
  • 3
  • 31
  • 55
  • I already downscale (as I've written, the input image isn't that large), down to 250dp. Thing is, I have no idea how come it got this crash. That's why I try to find a solution. It's very weird that so many devices have it working fine, yet some have crashes of OOM of almost 2GB... – android developer Jul 29 '15 at 16:34
  • As I understand you've posted the fix you've tried to avoid this crash and have troubles with left top side of image? Could you post code that produce this crash? – dilix Jul 29 '15 at 21:37
  • The code is already written. That's what's weird here... I wanted to see if I can make it better somehow. Maybe I should have asked how could it be that it happens at all, but then people would ask why don't I downsample, yet I already do... – android developer Jul 29 '15 at 22:57
  • I see only code with initial size of srcBitmap. How have you tried to downscale your image? http://stackoverflow.com/a/16408737/527808 – dilix Jul 30 '15 at 09:49