From d09ca482421c52411c4a1d8efcc004c8ac08c21b Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Tue, 12 Apr 2022 11:36:18 +0800 Subject: [PATCH] fix:#1774 --- .../book/read/page/provider/ImageProvider.kt | 53 +++++++------------ 1 file changed, 18 insertions(+), 35 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 0c1ed6674..f9f7e3b4f 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 @@ -13,6 +13,7 @@ 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 kotlinx.coroutines.runBlocking import splitties.init.appCtx import java.io.File @@ -20,6 +21,15 @@ import java.io.FileOutputStream object ImageProvider { + private val errorBitmap: Bitmap? by lazy { + BitmapUtils.decodeBitmap( + appCtx, + R.drawable.image_loading_error, + ChapterProvider.visibleWidth, + ChapterProvider.visibleHeight + ) + } + fun getImage( book: Book, src: String, @@ -41,41 +51,14 @@ object ImageProvider { } } } - return ImageLoader.loadBitmap(appCtx, vFile.absolutePath) - .error(R.drawable.image_loading_error) - .listener(glideListener) - .submit() - .get() - } - - private val glideListener by lazy { - object : RequestListener { - - override fun onLoadFailed( - e: GlideException?, - model: Any?, - target: Target?, - isFirstResource: Boolean - ): Boolean { - Coroutine.async { - (model as? String)?.let { path -> - File(path).delete() - } - } - return false - } - - override fun onResourceReady( - resource: Bitmap?, - model: Any?, - target: Target?, - dataSource: DataSource?, - isFirstResource: Boolean - ): Boolean { - return false - } - - } + return try { + ImageLoader.loadBitmap(appCtx, vFile.absolutePath).submit().get() + } catch (e: Exception) { + Coroutine.async { vFile.delete() } + //must call this method on a background thread + //ImageLoader.loadBitmap(appCtx, R.drawable.image_loading_error).submit().get() + errorBitmap + } } }