使用Glide Library将不同尺寸的图像加载到ImageView中。但不确定为什么滑行无法处理内存消耗,回收和重用它。
摇篮:
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”作为包名忽略它。
答案 0 :(得分:1)
尝试RequestOptions
& 升级版本。
RequestOptions requestOptions = new RequestOptions();
Glide.with(getContext())
.setDefaultRequestOptions(requestOptions)
.load(imageUrl)
.into(view);
build.gradle
应为
dependencies {
implementation 'com.github.bumptech.glide:glide:4.6.1'
}
您可以阅读Cannot obtain size for recycled Bitmap (LazyBitmapDrawableResource)
。