App is crashing which swiping in carousel view in maui

Vaibhav Methuku 165 Reputation points
2025-03-19T10:07:20.5066667+00:00

I had implemented the carousel view in android, When im swipping app is getting crashed.

Developer technologies | .NET | .NET MAUI
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 995 Reputation points Microsoft External Staff
    2025-08-12T07:17:30.9466667+00:00

    Hello,

    Java.Lang.RuntimeException: 'Canvas: trying to use a recycled bitmap android.graphics.Bitmap@50d15db'

    This crash occurs when Android tries to draw a Bitmap that has already been disposed or recycled, while some view or drawable still holds a reference.

    In a CarouselView, item views are reused during swipes. If a Bitmap (or its backing stream) is shared, disposed too early, or reassigned late to a recycled view, the renderer can hit this error.

    Recommended fixes

    I recommend implementing the following practices. These are designed to ensure proper management of Bitmap lifecycles and prevent conflicts during view recycling:

    • Avoid manual recycling while bound
      Do not call Bitmap.Recycle() or Bitmap.Dispose() on images still attached to an Image/ImageView. Let the loader/GC manage it unless the view is detached (see detachment note below).
    • Do not share mutable Bitmap instances
      Don’t assign the same Bitmap object to multiple views/pages. If you must share, copy it:
        var bmpCopy = original.Copy(original.GetConfig() ?? Bitmap.Config.Argb8888, false);
      
    • Prefer ImageSource over raw Bitmaps in MAUI
      Use FileImageSource, UriImageSource, or StreamImageSource instead of injecting Android Bitmaps into handlers. For streams, return a fresh stream each time:
        ImageSource.FromStream(() => File.OpenRead(path));
      
    • Cancel in-flight loads on recycle
      Keep a CancellationTokenSource in your item view/control. On OnBindingContextChanged or Unloaded, cancel pending loads before starting new ones to avoid late assignments on recycled views.
    • Detach before disposal (when you manage bitmaps)
      On Android, call imageView.SetImageDrawable(null); to detach the image, ensure no other view references it, then dispose the old Bitmap.
    • Clear sources on unbind/unload
      In templates, set Image.Source = null during Unloaded or when the BindingContext changes to release references as items leave the viewport.
    • Downsample large images
      Decode to target dimensions to reduce memory pressure. When decoding manually, use BitmapFactory.Options with InJustDecodeBounds then set InSampleSize.
    • Use fresh, open streams
      With StreamImageSource, don’t close or reuse a single MemoryStream. The factory delegate must provide a new, open stream on each call.
    • Avoid assigning the same bitmap during swipes
      Because CarouselView reuses views, ensure each binding gets its own data/stream to prevent concurrent reuse.

    Implementation checklist

    Since I don't have any information about your codebase, you could review the following checklist:

    • Search for Bitmap.Recycle(), Bitmap.Dispose(), or using around UI-bound images; move disposal to post-detachment points.
    • Verify no static/shared bitmap caches are recycled while visible.
    • Ensure StreamImageSource factories always return new streams and don’t close them prematurely.
    • Add cancellation logic to image loads in item templates; clear Image.Source on Unloaded/context changes.
    • Downsample large images before creating bitmaps/streams to match display needs.

    References

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.