【发布时间】: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