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

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

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

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

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

@ -10,7 +10,6 @@ import android.graphics.Color
import com.google.android.renderscript.Toolkit import com.google.android.renderscript.Toolkit
import java.io.FileInputStream import java.io.FileInputStream
import java.io.IOException import java.io.IOException
import java.io.InputStream
import kotlin.math.* 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 设备上下文 * @param context 设备上下文

Loading…
Cancel
Save