【发布时间】:2018-03-25 00:23:33
【问题描述】:
我有一个 JAVA(在 Android 上运行)方法,尽管变量 inSampleSize 在循环之前设置为 1,并且每次仅乘以 2,但由于除以零,偶尔会捕获 ArithmeticException。以下是原样的方法。知道我在这里想念什么吗?谢谢!
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
try {
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
} catch (ArithmeticException e) {
LogUtil.Log("ArithmeticException=" + e.getMessage() + " inSampleSize=" + inSampleSize);
Crashlytics.logException(e);
inSampleSize=1;
}
LogUtil.Log("inSampleSize="+inSampleSize);
return inSampleSize;
}
【问题讨论】:
-
抛出异常时记录了什么?
-
除以零。登录 inSampleSize 说它确实是零。看不出原因……
-
你有没有意识到出了什么问题?我有同样的问题,很难解释