From 8875e0a7598651e22957f69ca3748b43f9d4ea65 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 13 Apr 2022 10:00:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/read/page/provider/ImageProvider.kt | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) 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 2864158d8..1b08a166f 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 @@ -1,6 +1,7 @@ package io.legado.app.ui.book.read.page.provider import android.graphics.Bitmap +import android.util.Size import io.legado.app.R import io.legado.app.constant.AppLog.putDebug import io.legado.app.data.entities.Book @@ -9,25 +10,67 @@ import io.legado.app.help.BookHelp import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.glide.ImageLoader import io.legado.app.model.localBook.EpubFile -import io.legado.app.utils.FileUtils import io.legado.app.utils.BitmapUtils +import io.legado.app.utils.FileUtils import io.legado.app.utils.isXml import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.suspendCancellableCoroutine import splitties.init.appCtx import java.io.File import java.io.FileOutputStream +import kotlin.coroutines.resume object ImageProvider { private val errorBitmap: Bitmap? by lazy { BitmapUtils.decodeBitmap( - appCtx, - R.drawable.image_loading_error, - ChapterProvider.visibleWidth, - ChapterProvider.visibleHeight + appCtx, + R.drawable.image_loading_error, + ChapterProvider.visibleWidth, + ChapterProvider.visibleHeight ) } + private suspend fun cacheImage( + book: Book, + src: String, + bookSource: BookSource? + ): File { + val vFile = BookHelp.getImage(book, src) + if (!vFile.exists()) { + if (book.isEpub()) { + EpubFile.getImage(book, src)?.use { input -> + val newFile = FileUtils.createFileIfNotExist(vFile.absolutePath) + @Suppress("BlockingMethodInNonBlockingContext") + FileOutputStream(newFile).use { output -> + input.copyTo(output) + } + } + } else { + BookHelp.saveImage(bookSource, book, src) + } + } + return vFile + } + + suspend fun getImageSize( + book: Book, + src: String, + bookSource: BookSource? + ): Size { + val file = cacheImage(book, src, bookSource) + return suspendCancellableCoroutine { block -> + kotlin.runCatching { + ImageLoader.loadBitmap(appCtx, file.absolutePath).submit() + .getSize { width, height -> + block.resume(Size(width, height)) + } + }.onFailure { + block.cancel(it) + } + } + } + fun getImage( book: Book, src: String,