pull/2718/head
kunfei 2 years ago
parent 11712836b7
commit e2e923aec1
  1. 9
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  2. 24
      app/src/main/java/io/legado/app/utils/RealPathUtil.kt

@ -1,6 +1,5 @@
package io.legado.app.ui.book.read.page.provider package io.legado.app.ui.book.read.page.provider
import android.annotation.SuppressLint
import android.graphics.Paint import android.graphics.Paint
import android.graphics.Typeface import android.graphics.Typeface
import android.net.Uri import android.net.Uri
@ -571,15 +570,15 @@ object ChapterProvider {
upLayout() upLayout()
} }
@SuppressLint("Recycle")
private fun getTypeface(fontPath: String): Typeface { private fun getTypeface(fontPath: String): Typeface {
return kotlin.runCatching { return kotlin.runCatching {
when { when {
fontPath.isContentScheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { fontPath.isContentScheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
val fd = appCtx.contentResolver appCtx.contentResolver
.openFileDescriptor(Uri.parse(fontPath), "r")!! .openFileDescriptor(Uri.parse(fontPath), "r")!!
.fileDescriptor .use {
Typeface.Builder(fd).build() Typeface.Builder(it.fileDescriptor).build()
}
} }
fontPath.isContentScheme() -> { fontPath.isContentScheme() -> {
Typeface.createFromFile(RealPathUtil.getPath(appCtx, Uri.parse(fontPath))) Typeface.createFromFile(RealPathUtil.getPath(appCtx, Uri.parse(fontPath)))

@ -115,26 +115,18 @@ object RealPathUtil {
e.printOnDebug() e.printOnDebug()
val file = File(context.cacheDir, "tmp") val file = File(context.cacheDir, "tmp")
val filePath = file.absolutePath val filePath = file.absolutePath
var input: FileInputStream? = null
var output: FileOutputStream? = null
try { try {
val pfd = return context.contentResolver.openFileDescriptor(filePathUri!!, "r")?.use {
context.contentResolver.openFileDescriptor(filePathUri!!, "r") val fd = it.fileDescriptor
?: return null FileInputStream(fd).use { fis ->
val fd = pfd.fileDescriptor FileOutputStream(filePath).use { fos ->
input = FileInputStream(fd) fis.copyTo(fos)
output = FileOutputStream(filePath) }
var read: Int }
val bytes = ByteArray(4096) File(filePath).absolutePath
while (input.read(bytes).also { read = it } != -1) {
output.write(bytes, 0, read)
} }
return File(filePath).absolutePath
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
} finally {
input?.close()
output?.close()
} }
} finally { } finally {
cursor?.close() cursor?.close()

Loading…
Cancel
Save