Horis 2 vuotta sitten
vanhempi
commit
1afaff9084

+ 23 - 13
app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt

@@ -249,10 +249,11 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
             val fontPath = ReadBookConfig.textFont
             val fontPath = ReadBookConfig.textFont
             if (fontPath.isNotEmpty()) {
             if (fontPath.isNotEmpty()) {
                 val fontName = FileUtils.getName(fontPath)
                 val fontName = FileUtils.getName(fontPath)
-                val fontBytes = fontPath.parseToUri().readBytes(requireContext())
-                fontBytes.let {
+                val fontInputStream =
+                    fontPath.parseToUri().inputStream(requireContext()).getOrNull()
+                fontInputStream?.use {
                     val fontExportFile = FileUtils.createFileIfNotExist(configDir, fontName)
                     val fontExportFile = FileUtils.createFileIfNotExist(configDir, fontName)
-                    fontExportFile.writeBytes(it)
+                    it.copyTo(fontExportFile.outputStream())
                     exportFiles.add(fontExportFile)
                     exportFiles.add(fontExportFile)
                 }
                 }
             }
             }
@@ -261,8 +262,10 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
                 val bgFile = File(ReadBookConfig.durConfig.bgStr)
                 val bgFile = File(ReadBookConfig.durConfig.bgStr)
                 if (bgFile.exists()) {
                 if (bgFile.exists()) {
                     val bgExportFile = File(FileUtils.getPath(configDir, bgName))
                     val bgExportFile = File(FileUtils.getPath(configDir, bgName))
-                    bgFile.copyTo(bgExportFile)
-                    exportFiles.add(bgExportFile)
+                    if (!bgExportFile.exists()) {
+                        bgFile.copyTo(bgExportFile)
+                        exportFiles.add(bgExportFile)
+                    }
                 }
                 }
             }
             }
             if (ReadBookConfig.durConfig.bgTypeNight == 2) {
             if (ReadBookConfig.durConfig.bgTypeNight == 2) {
@@ -270,8 +273,10 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
                 val bgFile = File(ReadBookConfig.durConfig.bgStrNight)
                 val bgFile = File(ReadBookConfig.durConfig.bgStrNight)
                 if (bgFile.exists()) {
                 if (bgFile.exists()) {
                     val bgExportFile = File(FileUtils.getPath(configDir, bgName))
                     val bgExportFile = File(FileUtils.getPath(configDir, bgName))
-                    bgFile.copyTo(bgExportFile)
-                    exportFiles.add(bgExportFile)
+                    if (!bgExportFile.exists()) {
+                        bgFile.copyTo(bgExportFile)
+                        exportFiles.add(bgExportFile)
+                    }
                 }
                 }
             }
             }
             if (ReadBookConfig.durConfig.bgTypeEInk == 2) {
             if (ReadBookConfig.durConfig.bgTypeEInk == 2) {
@@ -279,8 +284,10 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
                 val bgFile = File(ReadBookConfig.durConfig.bgStrEInk)
                 val bgFile = File(ReadBookConfig.durConfig.bgStrEInk)
                 if (bgFile.exists()) {
                 if (bgFile.exists()) {
                     val bgExportFile = File(FileUtils.getPath(configDir, bgName))
                     val bgExportFile = File(FileUtils.getPath(configDir, bgName))
-                    bgFile.copyTo(bgExportFile)
-                    exportFiles.add(bgExportFile)
+                    if (!bgExportFile.exists()) {
+                        bgFile.copyTo(bgExportFile)
+                        exportFiles.add(bgExportFile)
+                    }
                 }
                 }
             }
             }
             val configZipPath = FileUtils.getPath(requireContext().externalCache, configFileName)
             val configZipPath = FileUtils.getPath(requireContext().externalCache, configFileName)
@@ -288,14 +295,17 @@ class BgTextConfigDialog : BaseDialogFragment(R.layout.dialog_read_bg_text) {
                 if (uri.isContentScheme()) {
                 if (uri.isContentScheme()) {
                     DocumentFile.fromTreeUri(requireContext(), uri)?.let { treeDoc ->
                     DocumentFile.fromTreeUri(requireContext(), uri)?.let { treeDoc ->
                         treeDoc.findFile(exportFileName)?.delete()
                         treeDoc.findFile(exportFileName)?.delete()
-                        treeDoc.createFile("", exportFileName)
-                            ?.writeBytes(requireContext(), File(configZipPath).readBytes())
+                        val out = treeDoc.createFile("", exportFileName)?.openOutputStream()
+                        out?.use {
+                            File(configZipPath).inputStream().use {
+                                it.copyTo(out)
+                            }
+                        }
                     }
                     }
                 } else {
                 } else {
                     val exportPath = FileUtils.getPath(File(uri.path!!), exportFileName)
                     val exportPath = FileUtils.getPath(File(uri.path!!), exportFileName)
                     FileUtils.delete(exportPath)
                     FileUtils.delete(exportPath)
-                    FileUtils.createFileIfNotExist(exportPath)
-                        .writeBytes(File(configZipPath).readBytes())
+                    File(configZipPath).copyTo(FileUtils.createFileIfNotExist(exportPath))
                 }
                 }
             }
             }
         }.onSuccess {
         }.onSuccess {

+ 4 - 4
app/src/main/java/io/legado/app/utils/FileDocExtensions.kt

@@ -212,7 +212,7 @@ fun FileDoc.createFileIfNotExist(
     vararg subDirs: String
     vararg subDirs: String
 ): FileDoc {
 ): FileDoc {
     return if (uri.isContentScheme()) {
     return if (uri.isContentScheme()) {
-        val documentFile = DocumentFile.fromTreeUri(appCtx, uri)!!
+        val documentFile = asDocumentFile()!!
         val tmp = DocumentUtils.createFileIfNotExist(documentFile, fileName, *subDirs)!!
         val tmp = DocumentUtils.createFileIfNotExist(documentFile, fileName, *subDirs)!!
         FileDoc.fromDocumentFile(tmp)
         FileDoc.fromDocumentFile(tmp)
     } else {
     } else {
@@ -226,7 +226,7 @@ fun FileDoc.createFolderIfNotExist(
     vararg subDirs: String
     vararg subDirs: String
 ): FileDoc {
 ): FileDoc {
     return if (uri.isContentScheme()) {
     return if (uri.isContentScheme()) {
-        val documentFile = DocumentFile.fromTreeUri(appCtx, uri)!!
+        val documentFile = asDocumentFile()!!
         val tmp = DocumentUtils.createFolderIfNotExist(documentFile, *subDirs)!!
         val tmp = DocumentUtils.createFolderIfNotExist(documentFile, *subDirs)!!
         FileDoc.fromDocumentFile(tmp)
         FileDoc.fromDocumentFile(tmp)
     } else {
     } else {
@@ -257,7 +257,7 @@ fun FileDoc.exists(
     vararg subDirs: String
     vararg subDirs: String
 ): Boolean {
 ): Boolean {
     return if (uri.isContentScheme()) {
     return if (uri.isContentScheme()) {
-        DocumentUtils.exists(DocumentFile.fromTreeUri(appCtx, uri)!!, fileName, *subDirs)
+        DocumentUtils.exists(asDocumentFile()!!, fileName, *subDirs)
     } else {
     } else {
         val path = FileUtils.getPath(uri.path!!, *subDirs) + File.separator + fileName
         val path = FileUtils.getPath(uri.path!!, *subDirs) + File.separator + fileName
         FileUtils.exist(path)
         FileUtils.exist(path)
@@ -266,7 +266,7 @@ fun FileDoc.exists(
 
 
 fun FileDoc.exists(): Boolean {
 fun FileDoc.exists(): Boolean {
     return if (uri.isContentScheme()) {
     return if (uri.isContentScheme()) {
-        DocumentFile.fromTreeUri(appCtx, uri)!!.exists()
+        asDocumentFile()!!.exists()
     } else {
     } else {
         FileUtils.exist(uri.path!!)
         FileUtils.exist(uri.path!!)
     }
     }