fresco icon indicating copy to clipboard operation
fresco copied to clipboard

error:trying to use a recycled bitmap

Open tianlang opened this issue 11 years ago • 11 comments

DataSource<CloseableReference<CloseableImage>> dataSource1 =
                    imagePipeline.fetchImageFromBitmapCache(imageRequest, CallerThreadExecutor.getInstance());
            try {
                CloseableReference<CloseableImage> imageReference = dataSource1.getResult().clone();
                if (imageReference != null) {
                    try {
                        CloseableBitmap image = (CloseableBitmap)imageReference.get();
                        // do something with the image
                        Bitmap loadedImage = image.getUnderlyingBitmap();
                        if (loadedImage != null) {
                            doSomethingWithBitmap(loadedImage);
                        } else {
                            Utils.showToast("error");
                        }
                    } finally {
                        CloseableReference.closeSafely(imageReference);
                    }
                }
            } finally {
                dataSource1.close();
            }

this code run first time was right,when I run this code twice or third time, the app report error:trying to use a recycled bitmap

第一次跑这段代码是正确的,图片可以正确的显示出来。然后在这个页面上再一次调用这个方法,图片地址也是一样的,就显示bitmap被回收了。

我想应该是我的写法有问题,请问该怎么写才能让bitmap不回收呢?

类似的问题 我也见到不少了,freso的大神们,你们为啥不详细得给一套解决方案呢?

tianlang avatar Nov 02 '15 06:11 tianlang

你是不是调用了Fresco.getImagePipeline().clearMemoryCaches()清理内存的方法

yamateh avatar Nov 02 '15 07:11 yamateh

没有

tianlang avatar Nov 02 '15 10:11 tianlang

#700 我发现你又post了一次。在700中,有说过你要有对象指向clone对象。又代码说话就是,你需要CloseableReference closeableImageReference = dataSource.getResult(); CloseableReference copyCloseableImageReference = closeableImageReference.clone(); CloseableBitmap closeableBitmap = (CloseableBitmap)copyCloseableImageReference.get(); 然后对closeableBitmap进行操作

中间每一步都需要有对象指向。不然gc会把bitmap回收。

aagnes-zz avatar Nov 02 '15 11:11 aagnes-zz

非常感谢你的回复,看到提醒之后,我立刻用你的代码试了一下, 然而并没有什么卵用。

tianlang avatar Nov 02 '15 13:11 tianlang

I am also having same issues please share if you resolve this issues Thanks

thizisarun avatar Nov 02 '15 14:11 thizisarun

如果你是想用imageview对bitmap进行展示的话,请使用drawee。不然,内存处理会比较麻烦。 对于上述的gc对bitmap回收的问题,代码如下,请结合你具体情况使用。

DataSubscriber dataSubscriber =
    new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
  @Override
  public void onNewResultImpl(
      DataSource<CloseableReference<CloseableImage>> dataSource) {

    if (!dataSource.isFinished()) {
      FLog.v("Not yet finished - this is just another progressive scan.");
    }  

    CloseableReference<CloseableImage> imageReference = dataSource.getResult();
    if (imageReference != null) {
      try {
        CloseableImage image = imageReference.get();
        // 此处对image进行操作,中间请自行判断bitmap是否被回收
Bitmap loadedImage = image.getUnderlyingBitmap();
doSomethingWithBitmap(loadedImage);
      } finally {
        imageReference.close();
      }
    }
  }
  @Override
  public void onFailureImpl(DataSource dataSource) {
    Throwable throwable = dataSource.getFailureCause();
    // handle failure
  }
};

dataSource.subscribe(dataSubscriber, executor);

aagnes-zz avatar Nov 02 '15 14:11 aagnes-zz

你给出的代码 跟我现在写的 其实并没有什么区别 加入这样一个判断: if (loadedImage != null && !loadedImage.isRecycled()){ doSomethingWithBitmap(loadedImage); }else{ Toast.makeText(WebViewActivity.this,"loadedImage.isRecycled",Toast.LENGTH_SHORT).show(); } 第二次以后 一定会走到else里面

//================= 不加入这个判断 还是报错 在doSomethingWithBitmap方法里 我有一个压缩bitmap的方法 报错就这里: java.lang.IllegalStateException: Can't compress a recycled bitmap at android.graphics.Bitmap.checkRecycled(Bitmap.java:393) at android.graphics.Bitmap.compress(Bitmap.java:1140) at .utils.ShareUtils.bmpToByteArray(ShareUtils.java:427) at .utils.ShareUtils.shareToTecent(ShareUtils.java:373) at .utils.ShareUtils.access$000(ShareUtils.java:51) at .utils.ShareUtils$2.onNewResultImpl(ShareUtils.java:234) at com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber.onNewResultImpl(BaseBitmapDataSubscriber.java:64) at com.facebook.datasource.BaseDataSubscriber.onNewResult(BaseDataSubscriber.java:43) at com.facebook.datasource.AbstractDataSource$1.run(AbstractDataSource.java:181) at com.facebook.common.executors.CallerThreadExecutor.execute(CallerThreadExecutor.java:49) at com.facebook.datasource.AbstractDataSource.notifyDataSubscriber(AbstractDataSource.java:172) at com.facebook.datasource.AbstractDataSource.subscribe(AbstractDataSource.java:155)

还有就是 我很好奇你们用的是什么版本的fresco CloseableImage image = imageReference.get(); Bitmap loadedImage = image.getUnderlyingBitmap();

这一段代码,我根本运行不了,CloseableImage并没有getUnderlyingBitmap方法,我必须强转为CloseableBitmap才可以

tianlang avatar Nov 03 '15 03:11 tianlang

I had the similar issue. I was trying to utilise the Bitmap in an imageview but in the finally block I was closing the reference CloseableReference.closeSafely(imageReference);

Could your issue be similar?

notsatyarth avatar Nov 03 '15 06:11 notsatyarth

yes

tianlang avatar Nov 03 '15 06:11 tianlang

好吧,仔细梳理了一遍代码,发现最终是我自己手动recycle了一次bitmap 闹了个乌龙,不好意思。 现在这个问题解决了,多谢。@aagnes

tianlang avatar Nov 03 '15 11:11 tianlang

I had the similar issue.

because i had recycle bitmap by my self

我也遇到这个问题,也是因为在使用bitmap过程中我自己的代码回收了bitmap,解决办法就是除了用于显示在view里,不要对bitmap做任何处理 希望能帮到你们

imcloud avatar Apr 28 '16 03:04 imcloud