【问题标题】:Facing error in loading image using Glide: Can't call reconfigure() on a recycled bitmap使用 Glide 加载图像时遇到错误:无法在回收的位图上调用 reconfigure()
【发布时间】:2018-05-14 14:28:15
【问题描述】:

我遇到错误

无法在回收的位图上调用 reconfigure()

使用 Glide 库加载图像时。 5 次中有 1 次出现此错误。图片大小约为 1.5MB。

我使用的是 3.8.0 版的 Glide。

这是我的转换代码:

public class ScaleToFitWidthHeightTransform extends BitmapTransformation {
int mSize = AppConstants.HEIGHT_TRANSFORM_LIMIT; //1020  
boolean isHeightScale;

public ScaleToFitWidthHeightTransform(Context context) {
    super(context);
}

public Bitmap transform(Bitmap source) {
    float scale;
    int newSize;
    int sourceHeight = source.getHeight();
    int sourceWidth = source.getWidth();

    // If original bitmap height/width is less then the height/width transform limit
    // then no need to scale the bitmap, so return the original bitmap
    if (sourceHeight < AppConstants.HEIGHT_TRANSFORM_LIMIT && sourceWidth < AppConstants.WIDTH_TRANSFORM_LIMIT) { // Height and width limit is 1020.
        return source;
    }
    Bitmap scalBitmap;

    if (sourceHeight > sourceWidth) {
        scale = (float) AppConstants.HEIGHT_TRANSFORM_LIMIT / source.getHeight();
        newSize = Math.round(source.getWidth() * scale);
        scaleBitmap = Bitmap.createScaledBitmap(source, newSize, mSize, true);
    } else {
        scale = (float) AppConstants.WIDTH_TRANSFORM_LIMIT / source.getWidth();
        newSize = Math.round(source.getHeight() * scale);
        scaleBitmap = Bitmap.createScaledBitmap(source, mSize, newSize, true);
    }
    if (scaleBitmap != source) {
        source.recycle();
    }

    return scaleBitmap;
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    return  transform(toTransform);
}

@Override
public String getId() {
    return "com.abc";
}

这是我使用 Glide 的行

        Glide.with(context)
                .load(imageUri).asBitmap()
                .transform(new ScaleToFitWidthHeightTransform(context))
                .placeholder(defaultDrawable)
                .error(defaultDrawable)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        BitmapSourceData bitmapSourceData = null;
                        bitmapSourceData = new BitmapSourceData();
                         bitmapSourceData.setBitmapSource(getBitmapBytes(resource));

                        if (imageView != null) {
                            imageView.setImageBitmap(resource);
                        }                           
                 }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            super.onLoadFailed(e, errorDrawable);
                            Log.e("ABC", "Exception --> " + e.toString());
                }); // Here I am getting error printed.

我在网上搜索。它说这是由于使用了回收的位图,但我无法修复它。 那我做错了什么?

【问题讨论】:

    标签: android bitmap transform android-glide


    【解决方案1】:

    你已经回收了这里的位图..

    if (scaleBitmap != source) {
        source.recycle();
    }
    

    不要这样做..

    另外createScaledBitmap 是一个非常繁重的操作,避免使用它。您可以使用画布和矩阵缩放位图..

    【讨论】:

    • 如果我不回收不需要的位图,可能会造成内存溢出异常。
    【解决方案2】:

    您不必回收位图。只需注意您的参考计数。如果没有更多对位图的引用,GC 将积极地启动并为您清理它。这只是一个标志,它可以更快地进行 GC。

    来自关于回收方法的位图文档:

    /** * 释放与此位图关联的本机对象,并清除 * 引用像素数据。这不会同步释放像素数据; * 如果没有其他引用,它只是允许它被垃圾收集。 * 位图被标记为“死”,这意味着如果它会抛出异常 * getPixels() 或 setPixels() 被调用,并且不会绘制任何内容。这个操作 * 不能反转,所以只有在你确定不存在的情况下才应该调用它 * 进一步用于位图。这是一个高级调用,通常需要 * 不会被调用,因为正常的 GC 进程会在 * 没有更多对该位图的引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2021-08-03
      相关资源
      最近更新 更多