Merge remote-tracking branch 'origin/master'

# Conflicts:
#	app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt
pull/1778/head
kunfei 3 years ago
commit 6287e899c0
  1. 61
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt

@ -1,22 +1,21 @@
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
import io.legado.app.data.entities.BookSource
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.BitmapUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.BitmapUtils
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 {
@ -29,46 +28,6 @@ object ImageProvider {
)
}
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,
@ -91,11 +50,17 @@ object ImageProvider {
}
}
return try {
ImageLoader.loadBitmap(appCtx, vFile.absolutePath).submit().get()
ImageLoader.loadBitmap(appCtx, vFile.absolutePath)
.submit(ChapterProvider.visibleWidth,ChapterProvider.visibleHeight)
.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()
Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete()
}
}
errorBitmap
}
}

Loading…
Cancel
Save