From cdda7c3191076068e1bab8afbb99c536d7ddfd74 Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Sat, 24 Apr 2021 01:39:47 +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 --- .../main/java/io/legado/app/help/BookHelp.kt | 27 +++++++++++++++-- .../io/legado/app/model/localBook/EpubFile.kt | 29 ++++--------------- .../legado/app/model/localBook/LocalBook.kt | 7 +++++ .../java/io/legado/app/utils/BitmapUtils.kt | 2 ++ .../java/me/ag2s/epublib/util/IOUtil.java | 2 +- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index febe400c2..dba76f97f 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -1,5 +1,6 @@ package io.legado.app.help +import android.net.Uri import io.legado.app.constant.AppPattern import io.legado.app.constant.EventBus import io.legado.app.data.appDb @@ -21,9 +22,9 @@ import kotlin.math.max import kotlin.math.min object BookHelp { - const val cacheFolderName = "book_cache" + private const val cacheFolderName = "book_cache" private const val cacheImageFolderName = "images" - val downloadDir: File = appCtx.externalFilesDir + private val downloadDir: File = appCtx.externalFilesDir private val downloadImages = CopyOnWriteArraySet() fun clearCache() { @@ -55,6 +56,28 @@ object BookHelp { } } + fun getEpubFile(book: Book,): File { + val file = FileUtils.getFile( + downloadDir, + cacheFolderName, + book.getFolderName(), + "index.epubx" + ) + if(!file.exists()){ + val input = if (book.bookUrl.isContentScheme()) { + val uri = Uri.parse(book.bookUrl) + appCtx.contentResolver.openInputStream(uri) + } else { + File(book.bookUrl).inputStream() + } + if (input != null) { + FileUtils.writeInputStream(file, input) + } + + } + return file + } + suspend fun saveContent(book: Book, bookChapter: BookChapter, content: String) { if (content.isEmpty()) return //保存文本 diff --git a/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt b/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt index 310f2263e..b80540f74 100644 --- a/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/EpubFile.kt @@ -2,12 +2,14 @@ package io.legado.app.model.localBook import android.graphics.Bitmap import android.graphics.BitmapFactory -import android.net.Uri import android.text.TextUtils import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp -import io.legado.app.utils.* +import io.legado.app.utils.FileUtils +import io.legado.app.utils.HtmlFormatter +import io.legado.app.utils.MD5Utils +import io.legado.app.utils.externalFilesDir import me.ag2s.epublib.domain.EpubBook import me.ag2s.epublib.epub.EpubReader import org.jsoup.Jsoup @@ -27,6 +29,7 @@ class EpubFile(var book: Book) { @Synchronized private fun getEFile(book: Book): EpubFile { + BookHelp.getEpubFile(book) if (eFile == null || eFile?.book?.bookUrl != book.bookUrl) { eFile = EpubFile(book) return eFile!! @@ -98,28 +101,8 @@ class EpubFile(var book: Book) { /*重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误*/ private fun readEpub(): EpubBook? { try { - //f - val file = FileUtils.getFile( - BookHelp.downloadDir, - BookHelp.cacheFolderName, - book.getFolderName(), - "index.epubx" - ) - if (!file.exists()) { - val input = if (book.bookUrl.isContentScheme()) { - val uri = Uri.parse(book.bookUrl) - appCtx.contentResolver.openInputStream(uri) - } else { - File(book.bookUrl).inputStream() - } - input ?: return null - FileUtils.writeInputStream(file, input) - if (!file.exists()){ - return EpubReader().readEpub(input) - } - - } + val file = BookHelp.getEpubFile(book) //通过懒加载读取epub return EpubReader().readEpubLazy(ZipFile(file), "utf-8") diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index a01efaee0..001cc93b5 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -138,6 +138,13 @@ object LocalBook { val bookFile = FileUtils.getFile(cacheFolder, book.originName) bookFile.delete() } + if(book.isEpub()){ + val bookFile=BookHelp.getEpubFile(book).parentFile + if (bookFile!=null&&bookFile.exists()){ + FileUtils.delete(bookFile,true) + } + + } if (deleteOriginal) { if (book.bookUrl.isContentScheme()) { diff --git a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt index 39d1e1534..bae066fe6 100644 --- a/app/src/main/java/io/legado/app/utils/BitmapUtils.kt +++ b/app/src/main/java/io/legado/app/utils/BitmapUtils.kt @@ -28,6 +28,7 @@ object BitmapUtils { */ fun decodeBitmap(path: String, width: Int, height: Int): Bitmap? { val op = BitmapFactory.Options() + op.inPreferredConfig=Config.RGB_565 var ips=FileInputStream(path) // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; op.inJustDecodeBounds = true @@ -54,6 +55,7 @@ object BitmapUtils { */ fun decodeBitmap(path: String): Bitmap? { val opts = BitmapFactory.Options() + opts.inPreferredConfig=Config.RGB_565 var ips=FileInputStream(path) opts.inJustDecodeBounds = true BitmapFactory.decodeStream(ips,null,opts) diff --git a/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java b/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java index 0ce72d062..dcbda6830 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java @@ -450,7 +450,7 @@ public class IOUtil { output.write(buffer, 0, n); count += n; } - input.close(); + //input.close(); } return count; }