From 1a8bdaf7e4df17f31949b630275ea13dbdb7c75f Mon Sep 17 00:00:00 2001 From: ag2s20150909 Date: Sat, 3 Apr 2021 00:57:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AE=BE=E7=BD=AE=E5=B0=81?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/cache/CacheActivity.kt | 2 +- .../app/ui/book/cache/CacheViewModel.kt | 33 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index e01100bfd..52feb9489 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -239,7 +239,7 @@ class CacheActivity : VMBaseActivity() adapter.getItem(exportPosition)?.let { book -> Snackbar.make(binding.titleBar, R.string.exporting, Snackbar.LENGTH_INDEFINITE) .show() - viewModel.export(path, book) { + viewModel.exportEPUB(path, book) { binding.titleBar.snackbar(it) } } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index 927b570ed..92b8bf238 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -1,8 +1,14 @@ package io.legado.app.ui.book.cache import android.app.Application +import android.content.Context +import android.graphics.Bitmap +import android.graphics.drawable.Drawable import android.net.Uri import androidx.documentfile.provider.DocumentFile +import com.bumptech.glide.Glide +import com.bumptech.glide.request.target.CustomTarget +import com.bumptech.glide.request.transition.Transition import io.legado.app.R import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppPattern @@ -16,6 +22,7 @@ import io.legado.app.utils.* import me.ag2s.epublib.domain.* import me.ag2s.epublib.epub.EpubWriter import me.ag2s.epublib.util.ResourceUtil +import java.io.ByteArrayOutputStream import java.io.File import java.io.FileOutputStream import java.nio.charset.Charset @@ -161,8 +168,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { //set metadata setEpubMetadata(book,epubBook) //set cover - //Todo - //epubBook.coverImage= Resource(FileInputStream(BookHelp.getImage(book, book.coverUrl)),"cover.jpg") + setCover(book, epubBook) //set css epubBook.resources.add( @@ -189,8 +195,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { //set metadata setEpubMetadata(book,epubBook) //set cover - //Todo - //epubBook.coverImage= Resource(FileInputStream(BookHelp.getImage(book, book.coverUrl)),"cover.jpg") + setCover(book, epubBook) //set css epubBook.resources.add( @@ -205,6 +210,26 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { setEpubContent(book, epubBook) EpubWriter().write(epubBook, FileOutputStream(bookFile)) } + private fun setCover(book: Book, epubBook: EpubBook) { + + Glide.with(context) + .asBitmap() + .load(book.coverUrl) + .into(object : CustomTarget(){ + override fun onResourceReady(resource: Bitmap, transition: Transition?) { + val stream = ByteArrayOutputStream() + resource.compress(Bitmap.CompressFormat.JPEG, 100, stream) + val byteArray: ByteArray = stream.toByteArray() + resource.recycle() + epubBook.coverImage= Resource(byteArray,"cover.jpg") + } + + override fun onLoadCleared(placeholder: Drawable?) { + + } + + }) + } private fun setEpubContent(book: Book, epubBook: EpubBook) { val useReplace = AppConfig.exportUseReplace