我有下面的代码来根据屏幕的大小缩放图像,这样它就不会被扭曲。我的问题是,当我将此函数放入转换中时,我总是收到此错误
java.lang.IllegalStateException: Transformation scale mutated input Bitmap but failed to recycle the original.
@SuppressWarnings("deprecation")
@Override
public Bitmap transform(Bitmap bitmap) {
if (bitmap != null) {
if (bitmap.getWidth() < MAX_WIDTH) {
bitmap = this.scaledDownBitmap(bitmap);
}
boolean flag = true;
int deviceWidth = ((Activity)this.context).getWindowManager().getDefaultDisplay().getWidth();
int deviceHeight = ((Activity)this.context).getWindowManager().getDefaultDisplay().getHeight();
int bitmapHeight = bitmap.getHeight(); // 563
int bitmapWidth = bitmap.getWidth(); // 900
// aSCPECT rATIO IS Always WIDTH x HEIGHT rEMEMMBER 1024 x 768
if (bitmapWidth > deviceWidth) {
flag = false;
// scale According to WIDTH
int scaledWidth = deviceWidth;
int scaledHeight = (scaledWidth * bitmapHeight) / bitmapWidth;
try {
if (scaledHeight > deviceHeight)
scaledHeight = deviceHeight;
bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
} catch (Exception e) {
e.printStackTrace();
}
}
if (flag) {
if (bitmapHeight > deviceHeight) {
// scale According to HEIGHT
int scaledHeight = deviceHeight;
int scaledWidth = (scaledHeight * bitmapWidth) / bitmapHeight;
try {
if (scaledWidth > deviceWidth)
scaledWidth = deviceWidth;
bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return bitmap;
}
最佳答案
您需要创建一个局部变量Bitmap,每次缩放位图时都会引用它,然后调用原始位图的回收方法来释放它的内存。
示例:
public Bitmap transform(Bitmap bitmap) {
Bitmap bitmap2;
Bitmap bitmap3;
if (bitmap != null) {
if (bitmap.getWidth() < MAX_WIDTH) {
bitmap = this.scaledDownBitmap(bitmap);
}
boolean flag = true;
int deviceWidth = ((Activity)this.context).getWindowManager().getDefaultDisplay().getWidth();
int deviceHeight = ((Activity)this.context).getWindowManager().getDefaultDisplay().getHeight();
int bitmapHeight = bitmap.getHeight(); // 563
int bitmapWidth = bitmap.getWidth(); // 900
// aSCPECT rATIO IS Always WIDTH x HEIGHT rEMEMMBER 1024 x 768
if (bitmapWidth > deviceWidth) {
flag = false;
// scale According to WIDTH
int scaledWidth = deviceWidth;
int scaledHeight = (scaledWidth * bitmapHeight) / bitmapWidth;
try {
if (scaledHeight > deviceHeight)
scaledHeight = deviceHeight;
bitmap2 = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true); //reference the scaled bitmap to local bitmap
bitmap.recycle(); //recycle the original bitmap
} catch (Exception e) {
e.printStackTrace();
}
}
if (flag) {
if (bitmapHeight > deviceHeight) {
// scale According to HEIGHT
int scaledHeight = deviceHeight;
int scaledWidth = (scaledHeight * bitmapWidth) / bitmapHeight;
try {
if (scaledWidth > deviceWidth)
scaledWidth = deviceWidth;
bitmap3 = Bitmap.createScaledBitmap(bitmap2, scaledWidth, scaledHeight, true);
bitmap2.recycle();
return bitmap3;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return bitmap2;
}
关于java - 变换比例变异了输入位图但未能回收原始位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24154856/