美文网首页
java.lang.IllegalStateException:

java.lang.IllegalStateException:

作者: 代码君_Coder | 来源:发表于2020-09-11 18:39 被阅读0次
    Caused by: java.lang.IllegalStateException: Cannot pool recycled bitmap
            at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.put(LruBitmapPool.java:86)
            at com.bumptech.glide.load.resource.bitmap.BitmapResource.recycle(BitmapResource.java:57)
            at com.bumptech.glide.load.engine.EngineResource.recycle(EngineResource.java:63)
            at com.bumptech.glide.load.engine.ResourceRecycler.recycle(ResourceRecycler.java:28)
            at com.bumptech.glide.load.engine.Engine.onResourceReleased(Engine.java:319)
            at com.bumptech.glide.load.engine.EngineResource.release(EngineResource.java:101)
            at com.bumptech.glide.load.engine.Engine.release(Engine.java:275)
            at com.bumptech.glide.request.SingleRequest.releaseResource(SingleRequest.java:290)
            at com.bumptech.glide.request.SingleRequest.clear(SingleRequest.java:269)
            at com.bumptech.glide.manager.RequestTracker.clearRemoveAndRecycle(RequestTracker.java:62)
            at com.bumptech.glide.RequestManager.untrack(RequestManager.java:437)
            at com.bumptech.glide.RequestManager.untrackOrDelegate(RequestManager.java:424)
            at com.bumptech.glide.RequestManager.clear(RequestManager.java:412)
            at com.bumptech.glide.RequestManager.onDestroy(RequestManager.java:272)
            at com.bumptech.glide.manager.ActivityFragmentLifecycle.onDestroy(ActivityFragmentLifecycle.java:64)
            at com.bumptech.glide.manager.SupportRequestManagerFragment.onDestroy(SupportRequestManagerFragment.java:187)
            at android.support.v4.app.Fragment.performDestroy(Fragment.java:2609)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1557)
    

    参考文献:https://www.jianshu.com/p/e7204506a9bb

    Glide源码:

    @Override
      public synchronized void put(Bitmap bitmap) {
        if (bitmap == null) {
          throw new NullPointerException("Bitmap must not be null");
        }
        if (bitmap.isRecycled()) {
          throw new IllegalStateException("Cannot pool recycled bitmap");
        }
        if (!bitmap.isMutable() || strategy.getSize(bitmap) > maxSize
            || !allowedConfigs.contains(bitmap.getConfig())) {
          bitmap.recycle();
          return;
        }
        final int size = strategy.getSize(bitmap);
        strategy.put(bitmap);//存入LruPoolStrategy中
        tracker.add(bitmap);
        puts++;
        currentSize += size;
        dump();//输出log信息
        evict();//根据currentSize计算是否超出maxSize,是则进行末位回收
      }
    

    通过阅读源码,第二个语句

    if (bitmap.isRecycled()) {
          throw new IllegalStateException("Cannot pool recycled bitmap");
    }
    

    大致意思是用来加载的bitmap被回收了,所以会报异常,按照这个思路,我检查自己的代码,发现确实当界面销毁的时候,我们的代码在Glide之前,主动回收了Bitmap,所以导致抛异常,所以解决办法很简单,只需要把自己主动回收的代码去除就好


    image.png

    如果你的代码也出现此问题,只需要搜索主动回收的代码recycle,注释掉即可,或者别用Glide,至于被回收会报异常出来,代码君还未读Glide里面的源码,不敢过分解读~,有了解的小伙伴可以在评论区里答疑一下

    相关文章

      网友评论

          本文标题:java.lang.IllegalStateException:

          本文链接:https://www.haomeiwen.com/subject/tiubektx.html