Blurry icon indicating copy to clipboard operation
Blurry copied to clipboard

Canvas: trying to use a recycled bitmap

Open MaSiYuan opened this issue 7 years ago • 11 comments

FATAL EXCEPTION: pool-30-thread-1 Process: com.test.app.activities, PID: 20021 java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@267f333 at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1271) at android.graphics.Canvas.drawBitmap(Canvas.java:1336) at jp.wasabeef.blurry.internal.Blur.of(Blur.java:63) at jp.wasabeef.blurry.internal.BlurTask$1.run(BlurTask.java:70) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:776)

MaSiYuan avatar Jul 13 '17 04:07 MaSiYuan

@wasabeef I am getting it too. Any idea why ?

Blurry.with(getActivity()).radius(10).sampling(8).animate(500).async().capture(linearLayout).into(imageview);

Edit I see it doesn't seem to work with viewgroup that I am trying to pass.

alexgomes09 avatar Aug 15 '17 18:08 alexgomes09

@MaSiYuan - were you able to make it work ?

alexgomes09 avatar Aug 15 '17 18:08 alexgomes09

I am getting this Crash too. @wasabeef any fix for this issue so far?

bharatdodeja avatar Jan 11 '18 06:01 bharatdodeja

i am getting this issue too.

MuhammadMuzammilSharif avatar Mar 04 '19 16:03 MuhammadMuzammilSharif

Having this issue +1

SiraWiz avatar Mar 11 '19 06:03 SiraWiz

Hi @wasabeef,

Also getting this issue too.

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@be3c212 at android.graphics.BaseCanvas.throwIfCannotDraw(BaseCanvas.java:55) at android.graphics.BaseCanvas.drawBitmap(BaseCanvas.java:102) at android.graphics.Canvas.drawBitmap(Canvas.java:1393) at c.a.a.a.a.a(Blur.java:63) at c.a.a.a.c$1.run(BlurTask.java:70)

Thanks, Hannah

ekahannah avatar May 02 '19 10:05 ekahannah

Hi @wasabeef,

Having the same issue 2019-08-19 21:53:37.016 6145-7209/com.xxx.xxx.xxx E/AndroidRuntime: FATAL EXCEPTION: pool-12-thread-5 Process: com.xxx.xxx.xxx, PID: 6145 java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@76d327 at android.graphics.BaseCanvas.throwIfCannotDraw(BaseCanvas.java:55) at android.graphics.BaseCanvas.drawBitmap(BaseCanvas.java:102) at android.graphics.Canvas.drawBitmap(Canvas.java:1393) at jp.wasabeef.blurry.internal.Blur.of(Blur.java:63) at jp.wasabeef.blurry.internal.BlurTask$1.run(BlurTask.java:70) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764)

Code used: Blurry.with(context) .radius(radius) .sampling(downscaleSampling) .async() .capture(sourceViewGroup) .into(destinationImageView);

Library version 3.0.0.

d-todor avatar Aug 20 '19 07:08 d-todor

Have the same issue on some rare cases. Still no answer/solution?

Xtizainy avatar Jan 09 '20 13:01 Xtizainy

My use case was blurring a layout into an image view using async to offload blurring work onto a background thread. The issue was a concurrent modification of the same view by multiple threads (due to Blurry internally using thread pool). In my case the solution was to make sure that two different threads are not blurring the same view concurrently. Also a try-catch block was added to catch the exception (does occur sometimes). Blurring kept working without any issues following the thrown exception.

The following code was used (uses RxJava for managing threads)

/**
   * Takes an image of the source {@link ViewGroup}, blurs it and sets to the target {@link
   * ImageView}. Blurring is performed on the background thread, produced bitmap is set on the UI
   * thread.
   *
   * @param radius               Radius used for blurring, higher value means that image will look
   *                             blurrier.
   * @param downscaleSampling    Downscale factor used during blurring. Higher value will provide
   *                             more efficient blurring, while the quality will degrade.
   * @param sourceViewGroup      ViewGroup which is used as source for blurring.
   * @param destinationImageView Destination image view which will receive blurred image of the
   *                             sourceViewGroup.
   * @return Layout blurring completable.
   */
  @SuppressWarnings("squid:S2221")
  public static Completable blurViewGroupIntoImageView(
      final int radius,
      final int downscaleSampling,
      @NonNull final ViewGroup sourceViewGroup,
      @NonNull final ImageView destinationImageView) {
    return Single.fromCallable(
        () -> {
          try {
            return blurViewGroupIntoBitmapDrawable(radius, downscaleSampling, destinationImageView,
                sourceViewGroup);
          } catch (Exception e) {
            throw new BlurringException(e);
          }
        })
        // Make sure that two threads are not blurring the same views concurrently.
        .subscribeOn(Schedulers.single())
        // Result of blurring is consumed on the main thread.
        .observeOn(AndroidSchedulers.mainThread())
        .flatMapCompletable(bitmapDrawable -> {
          destinationImageView.setImageDrawable(bitmapDrawable);
          return Completable.complete();
        });
  }

  private static BitmapDrawable blurViewGroupIntoBitmapDrawable(
      final int radius,
      final int downscaleSampling,
      @NonNull final ImageView destinationImageView,
      @NonNull final ViewGroup sourceViewGroup) {
    final BlurFactor factor = new BlurFactor();
    factor.sampling = downscaleSampling;
    factor.radius = radius;
    final Bitmap bitmap = BitmapHelper.createBitmap(sourceViewGroup);
    factor.width = sourceViewGroup.getWidth();
    factor.height = sourceViewGroup.getHeight();
    return new BitmapDrawable(destinationImageView.getResources(),
        Blur.of(destinationImageView.getContext(), bitmap, factor));
  }

Hope this helps.

d-todor avatar Jan 15 '20 15:01 d-todor

Any updates on this ?

muarachmann avatar Sep 28 '20 21:09 muarachmann

Same problem here, happens occasionally.

viewModel.card.observe(this, Observer { card ->
        ...
        lifecycleScope.launch {
            delay(50) //<--- If I remove this line the blur is randomly applied. 🤷‍♂️ 🤔 
            with(binding.barcodeImageView) {
                Blurry.with(this@AccountActivity)
                    .radius(10)
                    .sampling(2)
                    .async()
                    .capture(this)
                    .into(this)
            }
        }
})

GuilhE avatar Feb 02 '21 17:02 GuilhE