epub封面完成

pull/274/head
gedoor 4 years ago
parent eeb611fd05
commit 3a0cf21635
  1. 5
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 25
      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. 6
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -9,6 +9,7 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.FileUtils
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.externalFilesDir
import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
@ -20,9 +21,7 @@ import kotlin.math.min
object BookHelp {
private const val cacheFolderName = "book_cache"
private val downloadDir: File =
App.INSTANCE.getExternalFilesDir(null)
?: App.INSTANCE.cacheDir
private val downloadDir: File = App.INSTANCE.externalFilesDir
private fun bookFolderName(book: Book): String {
return formatFolderName(book.name) + MD5Utils.md5Encode16(book.bookUrl)

@ -1,16 +1,18 @@
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.App
import io.legado.app.data.entities.BookChapter
import io.legado.app.utils.htmlFormat
import io.legado.app.utils.isContentPath
import io.legado.app.utils.*
import nl.siegmann.epublib.domain.Book
import nl.siegmann.epublib.domain.TOCReference
import nl.siegmann.epublib.epub.EpubReader
import org.jsoup.Jsoup
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.nio.charset.Charset
import java.util.*
@ -19,6 +21,7 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) {
companion object {
private var eFile: EPUBFile? = null
private val coverDir = App.INSTANCE.externalFilesDir
@Synchronized
private fun getEFile(book: io.legado.app.data.entities.Book): EPUBFile {
@ -53,7 +56,25 @@ class EPUBFile(val book: io.legado.app.data.entities.Book) {
File(book.bookUrl).inputStream()
}
epubBook = epubReader.readEpub(inputStream)
if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = FileUtils.getPath(
App.INSTANCE.externalFilesDir,
"${MD5Utils.md5Encode16(book.bookUrl)}.jpg",
"covers"
)
}
if (!File(book.coverUrl!!).exists()) {
epubBook!!.coverImage?.inputStream?.let {
val cover = BitmapFactory.decodeStream(it)
val out = FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
cover.compress(Bitmap.CompressFormat.JPEG, 90, out)
out.flush()
out.close()
it.close()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}

@ -7,6 +7,8 @@ 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.FileUtils
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.externalFilesDir
import io.legado.app.utils.isContentPath
import java.io.File
@ -57,7 +59,12 @@ object LocalBook {
bookUrl = path,
name = name,
author = author,
originName = fileName
originName = fileName,
coverUrl = FileUtils.getPath(
App.INSTANCE.externalFilesDir,
"${MD5Utils.md5Encode16(path)}.jpg",
"covers"
)
)
App.db.bookDao().insert(book)
}

@ -20,6 +20,7 @@ import androidx.core.content.edit
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder
import com.google.zxing.EncodeHintType
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import io.legado.app.App
import io.legado.app.BuildConfig
import io.legado.app.R
import org.jetbrains.anko.defaultSharedPreferences
@ -157,7 +158,10 @@ val Context.sysBattery: Int
val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
val batteryStatus = registerReceiver(null, iFilter)
return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
}
}
val Context.externalFilesDir: File
get() = App.INSTANCE.getExternalFilesDir(null) ?: App.INSTANCE.filesDir
fun Context.openUrl(url: String) {
openUrl(Uri.parse(url))

Loading…
Cancel
Save