Caused by: java.lang.IllegalStateException: Cannot pool recycled bitmap

Caused by: java.lang.IllegalStateException: Cannot pool recycled bitmap
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.put(LruBitmapPool.java:108)
at com.bumptech.glide.load.resource.bitmap.BitmapResource.recycle(BitmapResource.java:57)
at com.bumptech.glide.load.engine.EngineResource.recycle(EngineResource.java:75)
at com.bumptech.glide.load.engine.ResourceRecycler.recycle(ResourceRecycler.java:22)
at com.bumptech.glide.load.engine.Engine.onResourceReleased(Engine.java:398)
at com.bumptech.glide.load.engine.EngineResource.release(EngineResource.java:116)
at com.bumptech.glide.load.engine.Engine.release(Engine.java:362)
at com.bumptech.glide.request.SingleRequest.clear(SingleRequest.java:326)
at com.bumptech.glide.manager.RequestTracker.clearAndRemove(RequestTracker.java:72)
at com.bumptech.glide.RequestManager.untrack(RequestManager.java:663)
at com.bumptech.glide.RequestManager.untrackOrDelegate(RequestManager.java:631)
at com.bumptech.glide.RequestManager.clear(RequestManager.java:627)
at com.bumptech.glide.RequestManager.onDestroy(RequestManager.java:374)
at com.bumptech.glide.manager.ActivityFragmentLifecycle.onDestroy(ActivityFragmentLifecycle.java:65)
at com.bumptech.glide.manager.SupportRequestManagerFragment.onDestroy(SupportRequestManagerFragment.java:215)
at androidx.fragment.app.Fragment.performDestroy(Fragment.java:2830)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1028)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchDestroy(FragmentManagerImpl.java:2644)
at androidx.fragment.app.FragmentController.dispatchDestroy(FragmentController.java:329)
at androidx.fragment.app.FragmentActivity.onDestroy(FragmentActivity.java:366)
at androidx.appcompat.app.AppCompatActivity.onDestroy(AppCompatActivity.java:233)
at com.scjl.littlemasteralbum.base.BaseActivity.onDestroy(BaseActivity.kt:79)
at com.scjl.littlemasteralbum.ui.activity.second.OtherDetailActivity.onDestroy(OtherDetailActivity.kt:195)
at android.app.Activity.performDestroy(Activity.java:8181)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1342)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4986)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5030)
at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)

记录一下Glide加载bitmap 报错的解决方式 Cannot pool recycled bitmap

发生问题原因:

我是在recycleview中使用SubsamplingScaleImageView组件,返回上个界面就会奔溃,就报此错,但是你使用正常的imageview就不会。

解决办法:

把bitmap转化为可变的 resource.copy(Bitmap.Config.ARGB_8888, true),加上此句就不会报这个错了。。

private fun displayImg(
        imageView: SubsamplingScaleImageView,
        data: ProductAllBean
    ) {
        var option = RequestOptions()
        option.diskCacheStrategy(DiskCacheStrategy.NONE)
        option.skipMemoryCache(true)
        var taget= object : CustomTarget<Bitmap>() {
            override fun onResourceReady(
                resource: Bitmap,
                transition: Transition<in Bitmap>?
            ) {
                if (resource != null) {
                    var mBitmap = resource.copy(Bitmap.Config.ARGB_8888, true)
                    imageView.setImage(ImageSource.bitmap(mBitmap))
                    imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM)
                }
            }
            override fun onLoadCleared(placeholder: Drawable?) {
            }
        }
        GlideApp.with(context).asBitmap().apply(option).load(data.thumbImageUrl).into(taget)

你可能感兴趣的:(android纠错)