From 1312bb038cca3a28dc81d459ea2d7e93e12b9293 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 14 Apr 2022 14:15:14 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/BookController.kt | 2 +- .../book/read/page/provider/ImageProvider.kt | 21 +++++++++++++++++++ .../java/io/legado/app/utils/BitmapUtils.kt | 16 ++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index 8771c8225..9254585f1 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -89,7 +89,7 @@ object BookController { this.bookUrl = bookUrl val bitmap = runBlocking { ImageProvider.cacheImage(book, src, bookSource) - ImageProvider.getImage(book, src, width, width) + ImageProvider.getImage(book, src, width) } return returnData.setData(bitmap) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt index 2de937b44..147d33e41 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt @@ -80,4 +80,25 @@ object ImageProvider { } } + fun getImage( + book: Book, + src: String, + width: Int + ): Bitmap { + val vFile = BookHelp.getImage(book, src) + @Suppress("BlockingMethodInNonBlockingContext") + return try { + BitmapUtils.decodeBitmap(vFile.absolutePath, width) + } catch (e: Exception) { + Coroutine.async { + putDebug("${vFile.absolutePath} 解码失败\n$e", e) + if (FileUtils.readText(vFile.absolutePath).isXml()) { + putDebug("${vFile.absolutePath}为xml,自动删除") + vFile.delete() + } + } + errorBitmap + } + } + } 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 9fbba9c02..fce4366f6 100644 --- a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt +++ b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt @@ -44,6 +44,22 @@ object BitmapUtils { 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 + } + op.inJustDecodeBounds = false + return BitmapFactory.decodeFile(path, op) + } + /** 从path中获取Bitmap图片 * @param path 图片路径 * @return From 374de8f7a5f2ee182e9b3d5596e67330355f67fa Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 14 Apr 2022 14:23:08 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/utils/BitmapUtils.kt | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) 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 fce4366f6..e3e3b3471 100644 --- a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt +++ b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt @@ -3,9 +3,11 @@ package io.legado.app.utils import android.content.Context -import android.graphics.* +import android.graphics.Bitmap import android.graphics.Bitmap.Config -import android.view.View +import android.graphics.BitmapFactory +import android.graphics.Color +import android.graphics.Matrix import com.google.android.renderscript.Toolkit import java.io.IOException import kotlin.math.* @@ -83,9 +85,7 @@ object BitmapUtils { fun decodeBitmap(context: Context, resId: Int): Bitmap? { val opt = BitmapFactory.Options() opt.inPreferredConfig = Config.RGB_565 - //获取资源图片 - val `is` = context.resources.openRawResource(resId) - return BitmapFactory.decodeStream(`is`, null, opt) + return BitmapFactory.decodeResource(context.resources, resId, opt) } /** @@ -96,13 +96,10 @@ object BitmapUtils { * @return */ fun decodeBitmap(context: Context, resId: Int, width: Int, height: Int): Bitmap? { - - var inputStream = context.resources.openRawResource(resId) - val op = BitmapFactory.Options() // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; op.inJustDecodeBounds = true - BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息 + BitmapFactory.decodeResource(context.resources, resId, op) //获取尺寸信息 //获取比例大小 val wRatio = ceil((op.outWidth / width).toDouble()).toInt() val hRatio = ceil((op.outHeight / height).toDouble()).toInt() @@ -114,9 +111,8 @@ object BitmapUtils { op.inSampleSize = hRatio } } - inputStream = context.resources.openRawResource(resId) op.inJustDecodeBounds = false - return BitmapFactory.decodeStream(inputStream, null, op) + return BitmapFactory.decodeResource(context.resources, resId, op) } /** @@ -135,31 +131,26 @@ object BitmapUtils { height: Int ): Bitmap? { var inputStream = context.assets.open(fileNameInAssets) - val op = BitmapFactory.Options() - // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; - op.inJustDecodeBounds = true - BitmapFactory.decodeStream(inputStream, 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 + return inputStream.use { + val op = BitmapFactory.Options() + // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; + op.inJustDecodeBounds = true + BitmapFactory.decodeStream(inputStream, 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 + } } + inputStream = context.assets.open(fileNameInAssets) + op.inJustDecodeBounds = false + BitmapFactory.decodeStream(inputStream, null, op) } - inputStream = context.assets.open(fileNameInAssets) - op.inJustDecodeBounds = false - return BitmapFactory.decodeStream(inputStream, null, op) - } - - //图片不被压缩 - fun convertViewToBitmap(view: View, bitmapWidth: Int, bitmapHeight: Int): Bitmap { - val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888) - view.draw(Canvas(bitmap)) - return bitmap } /** From cc1768bd665fc57cf2d0b40567eb5bd0c840c473 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 14 Apr 2022 14:25:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/help/storage/BackupConfig.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt b/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt index aa9a2a8ff..6658f7735 100644 --- a/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt +++ b/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt @@ -26,7 +26,8 @@ object BackupConfig { PreferKey.webDavUrl, PreferKey.webDavDir, PreferKey.webDavAccount, - PreferKey.webDavPassword + PreferKey.webDavPassword, + PreferKey.launcherIcon ) //配置忽略标题 From 26d1bb56d383dbfb07efc03f02a3ea52e5e488fa Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Thu, 14 Apr 2022 16:07:52 +0800 Subject: [PATCH 4/4] 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) + } + } /**