From 08f59704636d2bb0ecb1f07dcd1b8225b367912d Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 22 Mar 2020 10:56:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/config/FileAssociationViewModel.kt | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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 bed99c025..80db1af37 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 @@ -4,10 +4,12 @@ import android.app.Application import android.content.Intent import android.net.Uri import android.text.TextUtils +import androidx.documentfile.provider.DocumentFile import io.legado.app.base.BaseViewModel import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.isJsonArray import io.legado.app.utils.isJsonObject +import io.legado.app.utils.readText import java.io.File class FileAssociationViewModel(application: Application) : BaseViewModel(application) { @@ -15,10 +17,18 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica val url: String //如果是普通的url,需要根据返回的内容判断是什么 if (uri.scheme == "file" || uri.scheme == "content") { - val file = File(uri.path.toString()) + val content = if (uri.scheme == "file") { + val file = File(uri.path.toString()) + if (file.exists()) { + file.readText() + } else { + null + } + } else { + DocumentFile.fromSingleUri(context, uri)?.readText(context) + } var scheme = "" - if (file.exists()) { - val content = file.readText() + if (content != null) { if (content.isJsonObject() || content.isJsonArray()) { //暂时根据文件内容判断属于什么 when {