From d7f7c919365dcd40c0e76fe587394a4dd56e9211 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 5 Sep 2020 21:04:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/read/config/BgTextConfigDialog.kt | 25 ++++++++++++++++--- .../java/io/legado/app/utils/UriExtensions.kt | 2 ++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt index c16660612..e38ab601e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt @@ -197,6 +197,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack { @Suppress("BlockingMethodInNonBlockingContext") private fun exportConfig(uri: Uri) { execute { + val exportFiles = arrayListOf() val configDirPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig") FileUtils.deleteFile(configDirPath) val configDir = FileUtils.createFolderIfNotExist(configDirPath) @@ -204,6 +205,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack { FileUtils.deleteFile(configExportPath) val configExportFile = FileUtils.createFileIfNotExist(configExportPath) configExportFile.writeText(GSON.toJson(ReadBookConfig.durConfig)) + exportFiles.add(configExportFile) val fontPath = ReadBookConfig.textFont if (fontPath.isNotEmpty()) { val fontName = FileUtils.getName(fontPath) @@ -211,6 +213,7 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack { fontBytes?.let { val fontExportFile = FileUtils.createFileIfNotExist(configDir, fontName) fontExportFile.writeBytes(it) + exportFiles.add(fontExportFile) } } if (ReadBookConfig.durConfig.bgType() == 2) { @@ -219,16 +222,30 @@ class BgTextConfigDialog : BaseDialogFragment(), FileChooserDialog.CallBack { if (bgFile.exists()) { val bgExportFile = FileUtils.createFileIfNotExist(configDir, bgName) bgFile.copyTo(bgExportFile) + exportFiles.add(bgExportFile) } } - val configZipPath = FileUtils.getPath(requireContext().eCacheDir, "readConfig.zip") - if (ZipUtils.zipFile(configDirPath, configZipPath)) { - uri.writeBytes(requireContext(), File(configZipPath).readBytes()) + val configFileName = "readConfig.zip" + val configZipPath = FileUtils.getPath(requireContext().eCacheDir, configFileName) + if (ZipUtils.zipFiles(exportFiles, File(configZipPath))) { + if (uri.isContentPath()) { + DocumentFile.fromTreeUri(requireContext(), uri)?.let { treeDoc -> + treeDoc.findFile(configFileName)?.delete() + treeDoc.createFile("", configFileName) + ?.writeBytes(requireContext(), File(configZipPath).readBytes()) + } + } else { + val exportPath = FileUtils.getPath(File(uri.toString()), configFileName) + FileUtils.deleteFile(exportPath) + FileUtils.createFileIfNotExist(exportPath) + .writeBytes(File(configZipPath).readBytes()) + } } }.onSuccess { toast("导出成功") }.onError { - toast("导出失败:${it.localizedMessage}") + it.printStackTrace() + longToast("导出失败:${it.localizedMessage}") } } diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt index 583f699f7..77c0fcaee 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -4,6 +4,8 @@ import android.content.Context import android.net.Uri import java.io.File +fun Uri.isContentPath() = this.toString().isContentPath() + @Throws(Exception::class) fun Uri.readBytes(context: Context): ByteArray? { if (this.toString().isContentPath()) {