Glide Java.lang.IllegalStateException:无法获取回收Bitmap的大小:android.graphics.Bitmap@a4f3bcf [896x157] ARGB_8888

时间:2018-04-09 17:52:44

标签: android kotlin android-glide

我有一个带有一些背景的回收者视图的活动。

有时滚动Recyclerview并按回按钮我会收到此错误。

我有两种背景(一种是模糊的)。

要加载我使用的正常背景:

fun AppCompatImageView.loadImage(resId: Int) {
    Glide.with(context)
            .load(AppCompatResources.getDrawable(context, resId))
            .into(this)
}

要加载我使用的模糊背景:

Glide.with(mContext)
  .applyDefaultRequestOptions(RequestOptions().override(backgroundWidth, backgroundHeight))
  .asBitmap()
  .load(R.drawable.card_dog_placeholder)
  .into(object : SimpleTarget<Bitmap>() {
                                override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap>?) {
                                    cardYouAreHeroBackgroundImageView.setImageBitmap(bitmap.blurRenderScript(mContext, CARD_BG_RADIUS))
                                }
                            })

fun Bitmap.blurRenderScript(context: Context, radius: Float): Bitmap {
    val rs = RenderScript.create(context)
    val blurredBitmap = copy(Bitmap.Config.ARGB_8888, true)

    val input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED)
    val output = Allocation.createTyped(rs, input.type)

    val script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs))
    script.setInput(input)

    script.setRadius(radius)
    script.forEach(output)
    output.copyTo(blurredBitmap)
    return blurredBitmap
}
  

java.lang.RuntimeException:无法销毁活动   {com.yourpackage / com.yourpackage.DashboardActivity}:   java.lang.IllegalStateException:无法获取回收的大小   位图:android.graphics.Bitmap@a4f3bcf [896x157] ARGB_8888 at   android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4383)   在   android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4401)   在android.app.ActivityThread.-wrap5(未知来源:0)at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1649)   在android.os.Handler.dispatchMessage(Handler.java:106)at   android.os.Looper.loop(Looper.java:164)at   android.app.ActivityThread.main(ActivityThread.java:6494)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)引起   by:java.lang.IllegalStateException:无法获取回收的大小   位图:android.graphics.Bitmap@a4f3bcf [896x157] ARGB_8888 at   com.bumptech.glide.util.Util.getBitmapByteSize(Util.java:78)at   com.bumptech.glide.load.resource.bitmap.UnitBitmapDecoder $ NonOwnedBitmapResource.getSize(UnitBitmapDecoder.java:49)   在   com.bumptech.glide.load.engine.EngineResource.getSize(EngineResource.java:60)   在   com.bumptech.glide.load.engine.cache.LruResourceCache.getSize(LruResourceCache.java:42)   在   com.bumptech.glide.load.engine.cache.LruResourceCache.getSize(LruResourceCache.java:13)   在com.bumptech.glide.util.LruCache.put(LruCache.java:124)at   com.bumptech.glide.load.engine.cache.LruResourceCache.put(LruResourceCache.java:13)   在   com.bumptech.glide.load.engine.Engine.onResourceReleased(Engine.java:326)   在   com.bumptech.glide.load.engine.EngineResource.release(EngineResource.java:112)   在com.bumptech.glide.load.engine.Engine.release(Engine.java:286)at   com.bumptech.glide.request.SingleRequest.releaseResource(SingleRequest.java:345)   在   com.bumptech.glide.request.SingleRequest.clear(SingleRequest.java:324)   在   com.bumptech.glide.manager.RequestTracker.clearRemoveAndMaybeRecycle(RequestTracker.java:73)   在   com.bumptech.glide.manager.RequestTracker.clearRemoveAndRecycle(RequestTracker.java:60)   在com.bumptech.glide.RequestManager.untrack(RequestManager.java:623)   在   com.bumptech.glide.RequestManager.untrackOrDelegate(RequestManager.java:591)   在com.bumptech.glide.RequestManager.clear(RequestManager.java:579)at   com.bumptech.glide.RequestManager.onDestroy(RequestManager.java:323)   在   com.bumptech.glide.manager.ActivityFragmentLifecycle.onDestroy(ActivityFragmentLifecycle.java:65)   在   com.bumptech.glide.manager.SupportRequestManagerFragment.onDestroy(SupportRequestManagerFragment.java:194)   在android.support.v4.app.Fragment.performDestroy(Fragment.java:2585)   在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1559)   在   android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752)   在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821)   在   android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3251)   在   android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:3242)   在   android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:265)   在   android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:390)   在   android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:209)   在android.app.Activity.performDestroy(Activity.java:7208)处   android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1249)   在   android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4370)   在   android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4401)   在android.app.ActivityThread.-wrap5(未知来源:0)at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1649)   在android.os.Handler.dispatchMessage(Handler.java:106)at   android.os.Looper.loop(Looper.java:164)at   android.app.ActivityThread.main(ActivityThread.java:6494)at   java.lang.reflect.Method.invoke(Native Method)at   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

确保您的代码中没有类似以下的语句:

Glide.with(getApplicationContext()).clear(target);
Glide.get(getApplicationContext()).getBitmapPool().clearMemory();

从文档中,getBitmapPool()返回用于临时存储{@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}的{​​{1}},以便可以重复使用它们以避免垃圾回收。

通常,当任何位图回收发生时,特别是当位图已经回收时,就会发生此错误。因此,如果需要将{@link android.graphics.Bitmap}包装在getBitmapPool()支票if

中,