diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 10f874cd6..edf0b4eba 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -22,40 +22,36 @@ import kotlinx.coroutines.withContext import splitties.init.appCtx import java.io.File import java.io.FileInputStream +import java.io.FileOutputStream object Restore { suspend fun restore(context: Context, path: String) { - withContext(IO) { + kotlin.runCatching { if (path.isContentScheme()) { DocumentFile.fromTreeUri(context, Uri.parse(path))?.listFiles()?.forEach { doc -> - for (fileName in Backup.backupFileNames) { - if (doc.name == fileName) { - DocumentUtils.readText(context, doc.uri).let { - FileUtils.createFileIfNotExist("${Backup.backupPath}${File.separator}$fileName") - .writeText(it) + if (Backup.backupFileNames.contains(doc.name)) { + context.contentResolver.openInputStream(doc.uri)?.use { inputStream -> + val file = File("${Backup.backupPath}${File.separator}${doc.name}") + FileOutputStream(file).use { outputStream -> + inputStream.copyTo(outputStream) } } } } } else { - try { - val file = File(path) - for (fileName in Backup.backupFileNames) { - file.getFile(fileName).let { - if (it.exists()) { - it.copyTo( - FileUtils.createFileIfNotExist("${Backup.backupPath}${File.separator}$fileName"), - true - ) - } - } + val dir = File(path) + for (fileName in Backup.backupFileNames) { + val file = dir.getFile(fileName) + if (file.exists()) { + val target = File("${Backup.backupPath}${File.separator}$fileName") + file.copyTo(target, true) } - } catch (e: Exception) { - e.printOnDebug() } } + }.onFailure { + AppLog.put("", it) } restoreDatabase() restoreConfig() @@ -129,7 +125,7 @@ object Restore { ThemeConfig.upConfig() } } catch (e: Exception) { - e.printOnDebug() + AppLog.put("恢复主题出错\n${e.localizedMessage}", e) } if (!BackupConfig.ignoreReadConfig) { //恢复阅读界面配置 @@ -142,7 +138,7 @@ object Restore { ReadBookConfig.initConfigs() } } catch (e: Exception) { - e.printOnDebug() + AppLog.put("恢复阅读界面出错\n${e.localizedMessage}", e) } try { val file = @@ -153,7 +149,7 @@ object Restore { ReadBookConfig.initShareConfig() } } catch (e: Exception) { - e.printOnDebug() + AppLog.put("恢复阅读界面出错\n${e.localizedMessage}", e) } } Preferences.getSharedPreferences(appCtx, path, "config")?.all?.let { map ->