From 26d1bb56d383dbfb07efc03f02a3ea52e5e488fa Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Thu, 14 Apr 2022 16:07:52 +0800 Subject: [PATCH] use decodeFileDescriptor replace decodeFile to avoid twice create FileInputStream and auto close FileInputStream --- .../app/help/http/cronet/CronetInterceptor.kt | 4 +- .../java/io/legado/app/utils/BitmapUtils.kt | 82 +++++++++++-------- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt b/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt index 74b685128..30176c3cd 100644 --- a/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt +++ b/app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt @@ -48,8 +48,8 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor { } else { OldCallback(request, call) } - buildRequest(request, callBack)?.let { - return callBack.waitForDone(it) + buildRequest(request, callBack)?.runCatching { + return callBack.waitForDone(this) } return null } diff --git a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt index e3e3b3471..1a276da89 100644 --- a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt +++ b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt @@ -9,6 +9,7 @@ import android.graphics.BitmapFactory import android.graphics.Color import android.graphics.Matrix import com.google.android.renderscript.Toolkit +import java.io.FileInputStream import java.io.IOException import kotlin.math.* @@ -27,39 +28,51 @@ object BitmapUtils { */ @Throws(IOException::class) fun decodeBitmap(path: String, width: Int, height: Int): Bitmap { - val op = BitmapFactory.Options() - // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; - op.inJustDecodeBounds = true - BitmapFactory.decodeFile(path, op) - //获取比例大小 - val wRatio = ceil((op.outWidth / width).toDouble()).toInt() - val hRatio = ceil((op.outHeight / height).toDouble()).toInt() - //如果超出指定大小,则缩小相应的比例 - if (wRatio > 1 && hRatio > 1) { - if (wRatio > hRatio) { - op.inSampleSize = wRatio - } else { - op.inSampleSize = hRatio + + val fis = FileInputStream(path) + return fis.use { + val op = BitmapFactory.Options() + // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; + op.inJustDecodeBounds = true + BitmapFactory.decodeFileDescriptor(fis.fd, null, op) + //获取比例大小 + val wRatio = ceil((op.outWidth / width).toDouble()).toInt() + val hRatio = ceil((op.outHeight / height).toDouble()).toInt() + //如果超出指定大小,则缩小相应的比例 + if (wRatio > 1 && hRatio > 1) { + if (wRatio > hRatio) { + op.inSampleSize = wRatio + } else { + op.inSampleSize = hRatio + } } + op.inJustDecodeBounds = false + BitmapFactory.decodeFileDescriptor(fis.fd, null, op) + } - op.inJustDecodeBounds = false - return BitmapFactory.decodeFile(path, op) } @Throws(IOException::class) fun decodeBitmap(path: String, width: Int): Bitmap { - val op = BitmapFactory.Options() - // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; - op.inJustDecodeBounds = true - BitmapFactory.decodeFile(path, op) - //获取比例大小 - val wRatio = ceil((op.outWidth / width).toDouble()).toInt() - //如果超出指定大小,则缩小相应的比例 - if (wRatio > 1) { - op.inSampleSize = wRatio + + val fis = FileInputStream(path) + + return fis.use { + val op = BitmapFactory.Options() + // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; + op.inJustDecodeBounds = true + + BitmapFactory.decodeFileDescriptor(fis.fd, null, op) + //获取比例大小 + val wRatio = ceil((op.outWidth / width).toDouble()).toInt() + //如果超出指定大小,则缩小相应的比例 + if (wRatio > 1) { + op.inSampleSize = wRatio + } + op.inJustDecodeBounds = false + BitmapFactory.decodeFileDescriptor(fis.fd, null, op) } - op.inJustDecodeBounds = false - return BitmapFactory.decodeFile(path, op) + } /** 从path中获取Bitmap图片 @@ -68,12 +81,17 @@ object BitmapUtils { */ @Throws(IOException::class) fun decodeBitmap(path: String): Bitmap { - val opts = BitmapFactory.Options() - opts.inJustDecodeBounds = true - BitmapFactory.decodeFile(path, opts) - opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) - opts.inJustDecodeBounds = false - return BitmapFactory.decodeFile(path, opts) + val fis = FileInputStream(path) + return fis.use { + val opts = BitmapFactory.Options() + opts.inJustDecodeBounds = true + + BitmapFactory.decodeFileDescriptor(fis.fd, null, opts) + opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) + opts.inJustDecodeBounds = false + BitmapFactory.decodeFileDescriptor(fis.fd, null, opts) + } + } /**