diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 48fb62a1a..6b948ac3c 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -1,6 +1,5 @@ package io.legado.app.ui.book.read.page.provider -import android.annotation.SuppressLint import android.graphics.Paint import android.graphics.Typeface import android.net.Uri @@ -571,15 +570,15 @@ object ChapterProvider { upLayout() } - @SuppressLint("Recycle") private fun getTypeface(fontPath: String): Typeface { return kotlin.runCatching { when { fontPath.isContentScheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { - val fd = appCtx.contentResolver + appCtx.contentResolver .openFileDescriptor(Uri.parse(fontPath), "r")!! - .fileDescriptor - Typeface.Builder(fd).build() + .use { + Typeface.Builder(it.fileDescriptor).build() + } } fontPath.isContentScheme() -> { Typeface.createFromFile(RealPathUtil.getPath(appCtx, Uri.parse(fontPath))) diff --git a/app/src/main/java/io/legado/app/utils/RealPathUtil.kt b/app/src/main/java/io/legado/app/utils/RealPathUtil.kt index 72121998c..3885e74d4 100644 --- a/app/src/main/java/io/legado/app/utils/RealPathUtil.kt +++ b/app/src/main/java/io/legado/app/utils/RealPathUtil.kt @@ -115,26 +115,18 @@ object RealPathUtil { e.printOnDebug() val file = File(context.cacheDir, "tmp") val filePath = file.absolutePath - var input: FileInputStream? = null - var output: FileOutputStream? = null try { - val pfd = - context.contentResolver.openFileDescriptor(filePathUri!!, "r") - ?: return null - val fd = pfd.fileDescriptor - input = FileInputStream(fd) - output = FileOutputStream(filePath) - var read: Int - val bytes = ByteArray(4096) - while (input.read(bytes).also { read = it } != -1) { - output.write(bytes, 0, read) + return context.contentResolver.openFileDescriptor(filePathUri!!, "r")?.use { + val fd = it.fileDescriptor + FileInputStream(fd).use { fis -> + FileOutputStream(filePath).use { fos -> + fis.copyTo(fos) + } + } + File(filePath).absolutePath } - return File(filePath).absolutePath } catch (e: IOException) { e.printStackTrace() - } finally { - input?.close() - output?.close() } } finally { cursor?.close()