diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt index c84497536..8f0f46d94 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.book.source.manage import android.app.Application +import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile import com.jayway.jsonpath.JsonPath @@ -15,6 +16,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.jetbrains.anko.toast import java.io.File +import java.net.URLEncoder class BookSourceViewModel(application: Application) : BaseViewModel(application) { @@ -156,9 +158,20 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { execute { - val file = File(path) - if (file.exists()) { - importSource(file.readText(), finally) + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + val newPath = Uri.encode(path, ":/.") + DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) + } else { + val file = File(path) + if (file.exists()) { + file.readText() + } else { + null + } + } + if (content != null) { + importSource(content, finally) } else { withContext(Dispatchers.Main) { finally("打开文件出错") diff --git a/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt index 80db1af37..02aab3668 100644 --- a/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/config/FileAssociationViewModel.kt @@ -45,7 +45,11 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica } if (TextUtils.isEmpty(scheme)) { execute { - LocalBook.importFile(uri.path.toString()) + if (uri.scheme == "content"){ + LocalBook.importFile(uri.toString()) + }else{ + LocalBook.importFile(uri.path.toString()) + } toast("添加本地文件成功${uri.path}") } return null @@ -54,8 +58,13 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica toast("文件不存在") return null } + // content模式下,需要传递完整的路径,方便后续解析 + url = if (uri.scheme == "content"){ + "yuedu://${scheme}/importonline?src=$uri" + }else{ + "yuedu://${scheme}/importonline?src=${uri.path}" + } - url = "yuedu://${scheme}/importonline?src=${uri.path}" } else if (uri.scheme == "yuedu") { url = uri.toString() } else { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index ec75e78ed..d11d04d4a 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.replacerule import android.app.Application +import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile import io.legado.app.App @@ -18,9 +19,20 @@ import java.io.File class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) { fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { execute { - val file = File(path) - if (file.exists()) { - importSource(file.readText(), finally) + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + val newPath = Uri.encode(path, ":/.") + DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) + } else { + val file = File(path) + if (file.exists()) { + file.readText() + } else { + null + } + } + if (content != null) { + importSource(content, finally) } else { withContext(Dispatchers.Main) { finally("打开文件出错") diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt index 300d7468c..b4f20e20a 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceViewModel.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.rss.source.manage import android.app.Application +import android.net.Uri import android.text.TextUtils import androidx.documentfile.provider.DocumentFile import com.jayway.jsonpath.JsonPath @@ -134,9 +135,21 @@ class RssSourceViewModel(application: Application) : BaseViewModel(application) fun importSourceFromFilePath(path: String, finally: (msg: String) -> Unit) { execute { - val file = File(path) - if (file.exists()) { - GSON.fromJsonArray(file.readText())?.let { + val content = if (path.isContentPath()) { + //在前面被解码了,如果不进行编码,中文会无法识别 + val newPath = Uri.encode(path, ":/.") + DocumentFile.fromSingleUri(context, Uri.parse(newPath))?.readText(context) + } else { + val file = File(path) + if (file.exists()) { + file.readText() + } else { + null + } + } + + if (null != content) { + GSON.fromJsonArray(content)?.let { App.db.rssSourceDao().insert(*it.toTypedArray()) } }