我正在使用SubsamplingScaleImageView和Glide来加载,缓存和显示图像:
SubsamplingScaleImageView mImageView;
Glide.with(this)
.load(url)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
mImageView.setImage(ImageSource.bitmap(
Bitmap.createBitmap(resource, x1, y1, x2 - x1, y2 - y1)));
}
});
虽然有效,但有时会在IllegalStateException: Can't copy a recycled bitmap
中崩溃Bitmap.createBitmap
。如何解决?
答案 0 :(得分:0)
出于某种原因,您的Bitmap
会被回收。要解决此问题,您可以执行检查,然后继续执行必要的操作:
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (!resource.isRecycled()) {
mImageView.setImage(ImageSource.bitmap(Bitmap.createBitmap(resource, x1, y1, x2 - x1, y2 - y1)));
}
}