Glide:无法复制回收的位图

时间:2017-04-19 14:09:11

标签: android image android-image android-glide image-loading

我正在使用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。如何解决?

1 个答案:

答案 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)));
    }
}