簡體   English   中英

獲取 ArithmeticException:除以零,但我沒有在任何地方除以零

[英]Getting ArithmeticException: divide by zero, but I'm not dividing by zero anywhere

基本上,我從用戶存儲中讀取圖像進行處理,在讀取圖像時,我正在計算 inSampleSize 以保存一些 memory,這是我的代碼:

fun calculateInSampleSize(
    options: BitmapFactory.Options,
    reqWidth: Int,
    reqHeight: Int
): Int {
    // Raw height and width of image
    val (height: Int, width: Int) = options.run { outHeight to outWidth }
    var inSampleSize = 1
    if (height > reqHeight || width > reqWidth) {
        val halfHeight: Int = height / 2
        val halfWidth: Int = 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
        }
    }
    return inSampleSize
}

但是,我得到 ArithmeticException:在 while 循環條件內除以零:

halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth

我認為 inSampleSize 永遠不會為零,因為它從 1 開始並在每次迭代中乘以 2。 誰能幫我看看這是怎么回事?

它只會讓一些用戶崩潰(到目前為止大約有 13 次崩潰),我無法重現它。

編輯,添加崩潰日志:

Non-fatal Exception: java.lang.ArithmeticException: divide by zero
       at myapp.myapp.myapp.utils.UtilsKt.calculateInSampleSize(Utils.kt:108)
       at myapp.myapp.myapp.utils.UtilsKt$resizeImageWithPixel$2.invokeSuspend(Utils.kt:464)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
       at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
       at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:570)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

在此處輸入圖像描述

我認為 inSampleSize 永遠不會為零,因為它從 1 開始並在每次迭代中乘以 2。 誰能幫我看看這是怎么回事?

我認為那是你錯的地方。 如果你讓你的 integer 溢出,你肯定可以乘以 2 足夠的次數得到零:

var inSampleSize = 1
while (inSampleSize != 0) {
    println("inSampleSize = $inSampleSize")
    inSampleSize *= 2
}
println("inSampleSize is 0 now!")

在這里親自看看: https://pl.kotl.in/uZHs7YQCW

inSampleSize = 1
inSampleSize = 2
inSampleSize = 4
inSampleSize = 8
inSampleSize = 16
inSampleSize = 32
inSampleSize = 64
inSampleSize = 128
inSampleSize = 256
inSampleSize = 512
inSampleSize = 1024
inSampleSize = 2048
inSampleSize = 4096
inSampleSize = 8192
inSampleSize = 16384
inSampleSize = 32768
inSampleSize = 65536
inSampleSize = 131072
inSampleSize = 262144
inSampleSize = 524288
inSampleSize = 1048576
inSampleSize = 2097152
inSampleSize = 4194304
inSampleSize = 8388608
inSampleSize = 16777216
inSampleSize = 33554432
inSampleSize = 67108864
inSampleSize = 134217728
inSampleSize = 268435456
inSampleSize = 536870912
inSampleSize = 1073741824
inSampleSize = -2147483648
inSampleSize is 0 now!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM