使用Glide库时崩溃 [英] Crash on using Glide library

查看:604
本文介绍了使用Glide库时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Glide库将不同大小的图像加载到ImageView中.但是不确定为什么glide无法处理内存消耗,无法回收再利用.

Loading different size of images into ImageView using Glide Library. But not sure why glide fails to handle the memory consumption, recycle and reuse it.

等级:

api'com.github.bumptech.glide:glide:4.3.0'

api 'com.github.bumptech.glide:glide:4.3.0'

用法:

@BindingAdapter({"imageUrl"})
public static void imageUrl(ImageView view, String imageUrl) {
    try {
        if (imageUrl!=null) {
            Glide.with(view.getContext()).load(imageUrl).into(view);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

崩溃日志:

 04-23 13:19:34.390 4758-4758/xx.xx.xxxE/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxx.xx.xxxx, PID: 4758
    java.lang.RuntimeException: Unable to destroy activity {xx.xx.xx/x.xx.xx.xxx.xxxxActivity}: java.lang.IllegalStateException: Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@e212eb2[12x12] ARGB_8888
        at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4603)
        at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4621)
        at android.app.ActivityThread.-wrap5(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1757)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.IllegalStateException: Cannot obtain size for recycled Bitmap: android.graphics.Bitmap@e212eb2[12x12] ARGB_8888
        at com.bumptech.glide.util.Util.getBitmapByteSize(Util.java:74)
        at com.bumptech.glide.load.engine.bitmap_recycle.SizeConfigStrategy.removeLast(SizeConfigStrategy.java:97)
        at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.trimToSize(LruBitmapPool.java:221)
        at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.evict(LruBitmapPool.java:116)
        at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.put(LruBitmapPool.java:112)
        at com.bumptech.glide.load.resource.bitmap.LazyBitmapDrawableResource.recycle(LazyBitmapDrawableResource.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.onResourceRemoved(Engine.java:326)
        at com.bumptech.glide.load.engine.cache.LruResourceCache.onItemEvicted(LruResourceCache.java:31)
        at com.bumptech.glide.load.engine.cache.LruResourceCache.onItemEvicted(LruResourceCache.java:11)
        at com.bumptech.glide.util.LruCache.trimToSize(LruCache.java:175)
        at com.bumptech.glide.util.LruCache.evict(LruCache.java:180)
        at com.bumptech.glide.util.LruCache.put(LruCache.java:135)
        at com.bumptech.glide.load.engine.cache.LruResourceCache.put(LruResourceCache.java:11)
        at com.bumptech.glide.load.engine.Engine.onResourceReleased(Engine.java:334)
        at com.bumptech.glide.load.engine.EngineResource.release(EngineResource.java:101)
        at com.bumptech.glide.load.engine.Engine.release(Engine.java:292)
        at com.bumptech.glide.request.SingleRequest.releaseResource(SingleRequest.java:337)
        at com.bumptech.glide.request.SingleRequest.clear(SingleRequest.java:316)
        at com.bumptech.glide.manager.RequestTracker.clearRemoveAndRecycle(RequestTracker.java:62)
        at com.bumptech.glide.RequestManager.untrack(RequestManager.java:462)
        at com.bumptech.glide.RequestManager.untrackOrDelegate(RequestManager.java:449)
        at com.bumptech.glide.RequestManager.clear(RequestManager.java:437)
        at com.bumptech.glide.RequestManager.onDestroy(RequestManager.java:290)
        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:2516)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1557)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3227)
        at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:3218)
        at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:262)
    04-23 13:19:34.391 4758-4758/xx.xxx.xxxE/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:362)
            at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:209)
            at android.app.Activity.performDestroy(Activity.java:7462)
            at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1255)
            at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4590)
                ... 9 more

*软件包名称中使用的"xxx"将其忽略.

*used "xxx" for package name ignore it.

推荐答案

尝试使用 RequestOptions & 升级版本.

Try with RequestOptions & Upgrade version.

RequestOptions requestOptions = new RequestOptions();
                 Glide.with(getContext())
                    .setDefaultRequestOptions(requestOptions)
                    .load(imageUrl)
                    .into(view);

build.gradle 应该是

build.gradle should be

 dependencies {
  implementation 'com.github.bumptech.glide:glide:4.6.1'
 }

您可以阅读 Cannot obtain size for recycled Bitmap (LazyBitmapDrawableResource) .

这篇关于使用Glide库时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆