pull/2114/head
kunfei 2 years ago
parent 62e9dec468
commit 38e2fe103a
  1. 13
      app/src/main/java/io/legado/app/model/localBook/EpubFile.kt
  2. 18
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@ -1,7 +1,6 @@
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,11 +84,13 @@ 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 ->
val cover = BitmapFactory.decodeStream(input) BitmapUtils.decodeBitmap(input)?.let { cover ->
val out = FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!)) val out =
cover.compress(Bitmap.CompressFormat.JPEG, 90, out) FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
out.flush() cover.compress(Bitmap.CompressFormat.JPEG, 90, out)
out.close() out.flush()
out.close()
}
} }
} }
} }

@ -10,6 +10,7 @@ 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.*
@ -81,6 +82,23 @@ 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