Search code examples
androidandroid-bitmaprenderscriptandroid-renderscriptandroid-blur-effect

Android: Renderscript error - Cannot update allocation from bitmap, sizes mismatch


I'm trying to load a photo from the web and perform a blur on it, outputting the blurred image as a separate bitmap. My code looks like this:

            URL url = new URL(myUrl);
            mNormalImage = BitmapFactory.decodeStream(url.openStream());

            final RenderScript rs = RenderScript.create( mContext );
            final Allocation input = Allocation.createFromBitmap( rs, mNormalImage, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT );
            final Allocation output = Allocation.createTyped( rs, input.getType() );
            final ScriptIntrinsicBlur script;
            script = ScriptIntrinsicBlur.create( rs, Element.U8_4( rs ) );
            script.setRadius( 3.f );
            script.setInput( input );
            script.forEach( output );
            output.copyTo( mBlurredImage );

and I'm getting the error:

android.renderscript.RSIllegalArgumentException: 
Cannot update allocation from bitmap, sizes mismatch

Why is this happening?


Solution

  • Where is mBlurredImage created? This is happening because the size of that bitmap doesn't match the input. You should create it using something like:

    Bitmap mBlurredImage = 
        Bitmap.createBitmap(
            mNormalImage.getWidth(),
            mNormalImage.getHeight(),
            mNormalImage.getConfig());