【发布时间】:2016-03-28 01:19:06
【问题描述】:
我正在尝试从图像视图中检索位图,然后对其进行缩放。但我收到以下错误:
"java.lang.IllegalArgumentException: width and height must be > 0"
我在网上搜索是因为在 imageView 完成测量并转换为位图之前,我尝试更新位图。任何人都可以帮我解决这个问题。我的代码如下:
profileImage.buildDrawingCache();
Bitmap bm = profileImage.getDrawingCache();
int widthPixel = bm.getWidth();
int heightPixel = bm.getHeight();
int basePixel;
float widthRatio;
float heightRatio;
if (widthPixel < heightPixel) {
basePixel = widthPixel;
}
else {
basePixel = heightPixel;
}
if (basePixel > 180) {
widthRatio = 180/basePixel;
heightRatio = 180/basePixel;
}
else {
widthRatio = 1;
heightRatio = 1;
}
Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
// bm.recycle();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();
编辑后的代码(现在出现“java.lang.IllegalArgumentException: value may not be null.”错误):
profileImage.buildDrawingCache();
bm = profileImage.getDrawingCache();
profileImage.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
profileImage.getViewTreeObserver().removeOnPreDrawListener(this);
widthPixel = profileImage.getMeasuredWidth();
heightPixel = profileImage.getMeasuredHeight();
if (widthPixel < heightPixel) {
basePixel = widthPixel;
}
else {
basePixel = heightPixel;
}
if (basePixel > 180) {
widthRatio = 180/basePixel;
heightRatio = 180/basePixel;
}
else {
widthRatio = 1;
heightRatio = 1;
}
Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true);
// bm.recycle();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();
image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");
return true;
}
});
【问题讨论】: