Merge pull request #2237 from Xwite/master

修复epub封面和加载
pull/2239/head
kunfei 2 years ago committed by GitHub
commit 56f3a414ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 9
      app/src/main/java/io/legado/app/model/localBook/EpubFile.kt
  3. 9
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  4. 3
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt
  5. 2
      app/src/main/java/io/legado/app/model/localBook/UmdFile.kt
  6. 18
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@ -31,6 +31,7 @@ object BookHelp {
private val downloadDir: File = appCtx.externalFiles
private const val cacheFolderName = "book_cache"
private const val cacheImageFolderName = "images"
private const val cacheEpubFolderName = "epub"
private val downloadImages = CopyOnWriteArraySet<String>()
fun clearCache() {
@ -55,15 +56,24 @@ object BookHelp {
*/
suspend fun clearInvalidCache() {
withContext(IO) {
val bookFolderNames = appDb.bookDao.all.map {
it.getFolderName()
}
val file = downloadDir.getFile(cacheFolderName)
file.listFiles()?.forEach { bookFile ->
val bookFolderNames = ArrayList<String>()
val originNames = ArrayList<String>()
appDb.bookDao.all.forEach {
bookFolderNames.add(it.getFolderName())
if (it.isEpub()) originNames.add(it.originName)
}
downloadDir.getFile(cacheFolderName)
.listFiles()?.forEach { bookFile ->
if (!bookFolderNames.contains(bookFile.name)) {
FileUtils.delete(bookFile.absolutePath)
}
}
downloadDir.getFile(cacheEpubFolderName)
.listFiles()?.forEach { epubFile ->
if (!originNames.contains(epubFile.name)) {
FileUtils.delete(epubFile.absolutePath)
}
}
}
}
@ -165,7 +175,8 @@ object BookHelp {
fun getEpubFile(book: Book): ZipFile {
val uri = Uri.parse(book.bookUrl)
if (uri.isContentScheme()) {
val path = FileUtils.getPath(downloadDir, cacheFolderName, book.getFolderName(), book.originName)
FileUtils.createFolderIfNotExist(downloadDir, cacheEpubFolderName)
val path = FileUtils.getPath(downloadDir, cacheEpubFolderName, book.originName)
val file = File(path)
val doc = DocumentFile.fromSingleUri(appCtx, uri)
?: throw IOException("文件不存在")

@ -1,6 +1,7 @@
package io.legado.app.model.localBook
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.text.TextUtils
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
@ -85,16 +86,14 @@ class EpubFile(var book: Book) {
if (!File(book.coverUrl!!).exists()) {
/*部分书籍DRM处理后,封面获取异常,待优化*/
it.coverImage?.inputStream?.use { input ->
BitmapUtils.decodeBitmap(input)?.let { cover ->
val out =
FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
val cover = BitmapFactory.decodeStream(input)
val out = FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
cover.compress(Bitmap.CompressFormat.JPEG, 90, out)
out.flush()
out.close()
}
}
}
}
} catch (e: Exception) {
e.printOnDebug()
}
@ -262,8 +261,6 @@ class EpubFile(var book: Book) {
}
}
}
book.latestChapterTitle = chapterList.lastOrNull()?.title
book.totalChapterNum = chapterList.size
return chapterList
}

@ -78,9 +78,12 @@ object LocalBook {
if (chapters.isEmpty()) {
throw TocEmptyException(appCtx.getString(R.string.chapter_list_empty))
}
val lh = LinkedHashSet(chapters)
lh.forEachIndexed { index, bookChapter -> bookChapter.index = index }
return ArrayList(lh)
val list = ArrayList(LinkedHashSet(chapters))
list.forEachIndexed { index, bookChapter -> bookChapter.index = index }
book.latestChapterTitle = list.last().title
book.totalChapterNum = list.size
book.save()
return list
}
fun getContent(book: Book, chapter: BookChapter): String? {

@ -77,9 +77,6 @@ class TextFile(private val book: Book) {
bookChapter.bookUrl = book.bookUrl
bookChapter.url = MD5Utils.md5Encode16(book.originName + index + bookChapter.title)
}
book.latestChapterTitle = toc.last().title
book.totalChapterNum = toc.size
book.save()
return toc
}

@ -112,8 +112,6 @@ class UmdFile(var book: Book) {
DebugLog.d(javaClass.name, chapter.url)
chapterList.add(chapter)
}
book.latestChapterTitle = chapterList.lastOrNull()?.title
book.totalChapterNum = chapterList.size
return chapterList
}

@ -10,7 +10,6 @@ import android.graphics.Color
import com.google.android.renderscript.Toolkit
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStream
import kotlin.math.*
@ -82,23 +81,6 @@ object BitmapUtils {
}
}
/** 从path中获取Bitmap图片
* @param path 图片路径
* @return
*/
@Throws(IOException::class)
fun decodeBitmap(inputStream: InputStream): Bitmap? {
return inputStream.use {
val opts = BitmapFactory.Options()
opts.inJustDecodeBounds = true
BitmapFactory.decodeStream(inputStream, null, opts)
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128)
opts.inJustDecodeBounds = false
BitmapFactory.decodeStream(inputStream, null, opts)
}
}
/**
* 以最省内存的方式读取本地资源的图片
* @param context 设备上下文

Loading…
Cancel
Save