安卓图片缩放中java.lang.IllegalArgumentException: width and height must be > 0错误

  第一次写博客,都是低级的错误,大神见笑了。

  今天做安卓加载图片到 ImageView,报了 一个 java.lang.IllegalArgumentException: width and height must be > 0错误,只能怪我英文不好,没看懂。先把我的代码贴出来。

 1 bmp = BitmapFactory.decodeStream(cr.openInputStream(uri));
 2 
 3 // 缩放图片
 4 WindowManager wm = getWindowManager();
 5 int width = wm.getDefaultDisplay().getWidth();
 6 int height = wm.getDefaultDisplay().getHeight();
 7 // 图片的宽高
 8 int photo_width = bmp.getWidth();
 9 int photo_height = bmp.getHeight();
10 // 缩放比例,如小于1就设置为1
11 int bili = (photo_width/width)>(photo_height/height)?(photo_width/width):(photo_height/height);
12 bili = bili>1?bili:1;
13 System.out.println("bili:"+bili);
14 Matrix matrix = new Matrix(); 
15 matrix.postScale(1/bili,1/bili); //长和宽放大缩小的比例
16 Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),bmp.getHeight(),matrix,true);
17 
18 System.out.println(resizeBmp.getWidth()+":"+resizeBmp.getHeight());
19 
20 img1.setImageBitmap(resizeBmp);

一直在15行报错,我就不明白了,最后百度 matrix.postScale 中出现了赋值,于是我恍然大悟, matrix.postScale 接收的浮点类型的数,而我给的整数类型, 于是我该成了 matrix.postScale(1f/bili,1f/bili); 就没报错了。后来我仔细想想,这里的计算不合理,前面计算比例的时候应该用float的。

  以后还是要养成好习惯,浮点类型的尽量写上 f。

 

转载于:https://www.cnblogs.com/gwqblog/p/4700339.html

FATAL EXCEPTION: main Process: com.mercusys.halo, PID: 31585 java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:1209) at android.graphics.Bitmap.createBitmap(Bitmap.java:1175) at android.graphics.Bitmap.createBitmap(Bitmap.java:1123) at android.graphics.Bitmap.createBitmap(Bitmap.java:1082) at com.tplink.mercusys.component.onboarding.view.EarthAnimationViewDelegate.initLine(EarthAnimationViewDelegate.kt:102) at com.tplink.mercusys.component.onboarding.view.EarthAnimationViewDelegate.access$initLine(EarthAnimationViewDelegate.kt:23) at com.tplink.mercusys.component.onboarding.view.EarthAnimationViewDelegate$initEarth$$inlined$addListener$default$1.onAnimationEnd(Animator.kt:141) at android.animation.Animator$AnimatorListener.onAnimationEnd(Animator.java:719) at android.animation.Animator$AnimatorCaller$$ExternalSyntheticLambda1.call(Unknown Source:4) at android.animation.Animator.callOnList(Animator.java:677) at android.animation.Animator.notifyListeners(Animator.java:616) at android.animation.Animator.notifyEndListeners(Animator.java:641) at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1306) at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1585) at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:328) at android.animation.AnimationHandler.-$$Nest$mdoAnimationFrame(Unknown Source:0) at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:86) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1542) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1553) at android.view.Choreographer.doCallbacks(Choreographer.java:1109) at android.view.Choreographer.doFrame(Choreographer.java:984) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1527) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:257) at android.os.Looper.loop(Looper.java:368) at android.app.ActivityThread.main(ActivityThread.java:8839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
05-11
package com.example.newviewapplication; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; public class CustomView extends View { private Paint mPainter; public CustomView(Context context) { super(context); init(); } public CustomView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); init(); } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray t = context.obtainStyledAttributes(attrs,R.styleable.CustomView); int mColor = t.getColor(R.styleable.CustomView_circle_color,Color.BLUE); t.recycle(); mPainter.setColor(mColor); init(); } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } private void init(){ mPainter = new Paint(); mPainter.setColor(Color.RED); mPainter.setStrokeWidth(5f); mPainter.setStyle(Paint.Style.FILL); } @Override protected void onDraw(@NonNull Canvas canvas) { super.onDraw(canvas); //padding生效 // 获取传入的padding值 final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); final int paddingTop = getPaddingTop(); final int paddingBottom = getPaddingBottom(); // 获取绘制内容的高度和宽度(考虑了四个方向的padding值) int width = getWidth() - paddingLeft - paddingRight ; int height = getHeight() - paddingTop - paddingBottom ; // 设置圆的半径 = 宽,高最小值的2分之1 int r = Math.min(width, height)/2; // 画出圆(蓝色) // 圆心 = 控件的中央,半径 = 宽,高最小值的2分之1 canvas.drawCircle(paddingLeft+width/2,paddingTop+height/2,r,mPainter); } //wrap_content生效 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 获取宽-测量规则的模式和大小 int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); // 获取高-测量规则的模式和大小 int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); // 设置wrap_content的默认宽 / 高值 // 默认宽/高的设定并无固定依据,根据需要灵活设置 // 类似TextView,ImageView等针对wrap_content均在onMeasure()对设置默认宽 / 高值有特殊处理,具体读者可以自行查看 int mWidth = 400; int mHeight = 400; // 当布局参数设置为wrap_content时,设置默认值 if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT && getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) { setMeasuredDimension(mWidth, mHeight); // 宽 / 高任意一个布局参数为= wrap_content时,都设置默认值 } else if (getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) { setMeasuredDimension(mWidth, heightSize); } else if (getLayoutParams().height == ViewGroup.LayoutParams.WRAP_CONTENT) { setMeasuredDimension(widthSize, mHeight); } } } 代码报错:FATAL EXCEPTION: main Process: com.example.newviewapplication, PID: 15829 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newviewapplication/com.example.newviewapplication.MainActivity}: android.view.InflateException: Binary XML file line #15 in com.example.newviewapplication:layout/activity_main: Binary XML file line #15 in com.example.newviewapplication:layout/activity_main: Error inflating class com.example.newviewapplication.CustomView
最新发布
07-29
### 问题分析与解决方法 在 Java 编程中,`java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String` 是一种常见的运行时异常。这种异常通常发生在尝试对 `java.util.Date` 和 `java.lang.String` 类型进行比较时[^1]。具体来说,当 SQL 查询条件中包含对日期字段的字符串判断逻辑(例如 `createTime = ' '`),而实际传递的参数是 `Date` 类型时,就会引发此异常[^2]。 为了解决这一问题,需要确保在代码中正确处理类型转换,并避免直接对不同类型的对象进行比较。以下是具体的解决方案: --- ### 解决方案 #### 1. 修改 XML 或 SQL 条件逻辑 如果问题出现在 MyBatis 的 XML 配置文件中,可以通过调整 `<if>` 标签的条件逻辑来避免类型不匹配的问题。例如,将以下代码: ```xml <if test="deadline != null and deadline != ''"> deadline = #{deadline}, </if> ``` 修改为仅检查 `deadline` 是否为 `null`,而不进行字符串比较: ```xml <if test="deadline != null"> deadline = #{deadline}, </if> ``` 这样可以有效避免 `java.util.Date` 和 `java.lang.String` 的类型冲突[^3]。 --- #### 2. 确保参数类型一致性 在传递参数到 SQL 查询之前,应确保所有参数的类型一致。例如,如果数据库中的字段是 `DATE` 类型,则应该传递 `java.util.Date` 或其子类(如 `java.sql.Date`)作为参数,而不是传递字符串。 可以在代码中显式地进行类型转换,例如使用 `SimpleDateFormat` 将字符串转换为日期: ```java import java.text.SimpleDateFormat; import java.util.Date; public class DateConverter { public static Date convertToDate(String dateString) throws Exception { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.parse(dateString); } } ``` 然后,在调用 SQL 查询之前,先将字符串参数转换为 `Date` 类型: ```java try { String deadlineStr = "2023-10-01"; Date deadlineDate = DateConverter.convertToDate(deadlineStr); // 将 deadlineDate 作为参数传递给 SQL 查询 } catch (Exception e) { e.printStackTrace(); } ``` --- #### 3. 使用 MyBatis 的类型处理器 MyBatis 提供了类型处理器(TypeHandler)机制,用于在 Java 对象和数据库字段之间进行类型转换。可以通过自定义类型处理器来确保 `java.util.Date` 和 `String` 之间的正确转换。 例如,定义一个自定义的类型处理器: ```java import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; public class DateStringTypeHandler extends BaseTypeHandler<Date> { private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); @Override public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException { ps.setString(i, DATE_FORMAT.format(parameter)); } @Override public Date getNullableResult(ResultSet rs, String columnName) throws SQLException { String dateStr = rs.getString(columnName); return dateStr == null ? null : parseDate(dateStr); } @Override public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String dateStr = rs.getString(columnIndex); return dateStr == null ? null : parseDate(dateStr); } @Override public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String dateStr = cs.getString(columnIndex); return dateStr == null ? null : parseDate(dateStr); } private Date parseDate(String dateStr) { try { return DATE_FORMAT.parse(dateStr); } catch (Exception e) { throw new RuntimeException("Failed to parse date string: " + dateStr, e); } } } ``` 接着,在 MyBatis 的配置文件中注册该类型处理器: ```xml <typeHandlers> <typeHandler handler="com.example.DateStringTypeHandler" javaType="java.util.Date" jdbcType="VARCHAR"/> </typeHandlers> ``` --- ### 总结 通过上述方法,可以有效解决 `java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String` 的问题。核心在于确保参数类型的一致性,并避免在代码或 SQL 中直接对不同类型的对象进行比较。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值