kunfei 3 лет назад
Родитель
Сommit
57f696dc06

+ 4 - 5
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)))

+ 8 - 16
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()