My application closing without exception when i try to canvas.DrawBitmap()

DmytroBabych.5059DmytroBabych.5059 USUniversity ✭✭
edited November 2015 in Xamarin.Android

My application just closing without exception when i draw bitmap on canvas:
How to draw bitmap correct?

public class BadgedImageRenderer : VisualElementRenderer<BadgedImage>
    {
        public BadgedImageRenderer()
        {
            this.SetWillNotDraw(false);
        }

    public async override void Draw(Canvas canvas)
        {
            // Draw base image
            base.Draw(canvas);
    var ctl = Element;
            if (!string.IsNullOrEmpty(ctl.Source))
            {
                Paint pnt = new Paint();

                    var bitmap = await GetBitmapAsync(ctl.Source);

                    if (bitmap != null)
                    {
                        var mutableBitmap = bitmap.Copy(Bitmap.Config.Argb8888, true);

                        canvas.DrawBitmap(mutableBitmap, 0, 0, pnt);
                }
            }
    }

    private async Task<Bitmap> GetBitmapAsync(ImageSource source)
        {
            var handler = GetHandler(source);
            var returnValue = (Bitmap)null;

            if (handler != null)
                returnValue = await handler.LoadImageAsync(source, this.Context);

            return returnValue;
        }

private static IImageSourceHandler GetHandler(ImageSource source)
        {
            IImageSourceHandler returnValue = null;
            if (source is UriImageSource)
            {
                returnValue = new ImageLoaderSourceHandler();
            }
            else if (source is FileImageSource)
            {
                returnValue = new FileImageSourceHandler();
            }
            else if (source is StreamImageSource)
            {
                returnValue = new StreamImagesourceHandler();
            }
            return returnValue;
        }

}

Answers

  • DmytroBabych.5059DmytroBabych.5059 USUniversity ✭✭
    edited November 2015

    Ok, It fixed when i recycle my bitmaps. Maybe it was out of memory exception.

    if (!string.IsNullOrEmpty (ctl.Source)) {
                    using (var bitmap = await GetBitmapAsync (ctl.Source)) {
    
                        if (bitmap != null) {
                            using (var mutableBitmap = bitmap.Copy (Bitmap.Config.Argb8888, true)) {
                                canvas.DrawBitmap (mutableBitmap, 0, 0, null);
                                mutableBitmap.Recycle ();
                                bitmap.Recycle ();
                            }
                        }
                    }
                }
    
Sign In or Register to comment.