优化
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package io.legado.app.help.book
|
||||
|
||||
import android.os.ParcelFileDescriptor
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.constant.AppLog
|
||||
import io.legado.app.constant.AppPattern
|
||||
@@ -198,6 +199,22 @@ object BookHelp {
|
||||
return ZipFile(uri.path)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本地书籍文件的ParcelFileDescriptor
|
||||
*
|
||||
* @param book
|
||||
* @return
|
||||
*/
|
||||
@Throws(IOException::class, FileNotFoundException::class)
|
||||
fun getBookPFD(book: Book): ParcelFileDescriptor? {
|
||||
val uri = book.getLocalUri()
|
||||
return if (uri.isContentScheme()) {
|
||||
appCtx.contentResolver.openFileDescriptor(uri, "r")
|
||||
} else {
|
||||
ParcelFileDescriptor.open(File(uri.path), ParcelFileDescriptor.MODE_READ_ONLY)
|
||||
}
|
||||
}
|
||||
|
||||
fun getChapterFiles(book: Book): HashSet<String> {
|
||||
val fileNames = hashSetOf<String>()
|
||||
if (book.isLocalTxt) {
|
||||
|
||||
@@ -8,10 +8,12 @@ import com.bumptech.glide.load.model.GlideUrl
|
||||
import com.bumptech.glide.module.AppGlideModule
|
||||
import java.io.InputStream
|
||||
|
||||
|
||||
@Suppress("unused")
|
||||
@GlideModule
|
||||
class LegadoGlideModule : AppGlideModule() {
|
||||
|
||||
|
||||
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
|
||||
registry.replace(
|
||||
GlideUrl::class.java,
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.legado.app.model.localBook
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.text.TextUtils
|
||||
import io.legado.app.constant.AppLog
|
||||
import io.legado.app.data.entities.Book
|
||||
@@ -12,6 +13,7 @@ import me.ag2s.epublib.domain.EpubBook
|
||||
import me.ag2s.epublib.domain.Resource
|
||||
import me.ag2s.epublib.domain.TOCReference
|
||||
import me.ag2s.epublib.epub.EpubReader
|
||||
import me.ag2s.epublib.util.zip.AndroidZipFile
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Element
|
||||
import org.jsoup.select.Elements
|
||||
@@ -65,9 +67,14 @@ class EpubFile(var book: Book) {
|
||||
}
|
||||
|
||||
private var mCharset: Charset = Charset.defaultCharset()
|
||||
|
||||
/**
|
||||
*持有引用,避免被回收
|
||||
*/
|
||||
private var fileDescriptor: ParcelFileDescriptor? = null
|
||||
private var epubBook: EpubBook? = null
|
||||
get() {
|
||||
if (field != null) {
|
||||
if (field != null && fileDescriptor != null) {
|
||||
return field
|
||||
}
|
||||
field = readEpub()
|
||||
@@ -107,8 +114,14 @@ class EpubFile(var book: Book) {
|
||||
private fun readEpub(): EpubBook? {
|
||||
return kotlin.runCatching {
|
||||
//ContentScheme拷贝到私有文件夹采用懒加载防止OOM
|
||||
val zipFile = BookHelp.getEpubFile(book)
|
||||
EpubReader().readEpubLazy(zipFile, "utf-8")
|
||||
//val zipFile = BookHelp.getEpubFile(book)
|
||||
BookHelp.getBookPFD(book)?.let {
|
||||
fileDescriptor = it
|
||||
val zipFile = AndroidZipFile(it, book.originName)
|
||||
EpubReader().readEpubLazy(zipFile, "utf-8")
|
||||
}
|
||||
|
||||
|
||||
}.onFailure {
|
||||
AppLog.put("读取Epub文件失败\n${it.localizedMessage}", it)
|
||||
it.printOnDebug()
|
||||
@@ -350,4 +363,9 @@ class EpubFile(var book: Book) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected fun finalize() {
|
||||
fileDescriptor?.close()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,10 +57,13 @@ class PdfFile(var book: Book) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*持有引用,避免被回收
|
||||
*/
|
||||
private var fileDescriptor: ParcelFileDescriptor? = null
|
||||
private var pdfRenderer: PdfRenderer? = null
|
||||
get() {
|
||||
if (field != null) {
|
||||
if (field != null && fileDescriptor != null) {
|
||||
return field
|
||||
}
|
||||
field = readPdf()
|
||||
@@ -180,7 +183,7 @@ class PdfFile(var book: Book) {
|
||||
val index = href.toInt()
|
||||
val bitmap = openPdfPage(pdfRenderer!!, index)
|
||||
if (bitmap != null) {
|
||||
BitmapUtils.toInputStream(bitmap).also { bitmap.recycle() }
|
||||
BitmapUtils.toInputStream(bitmap)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user