From e64a8aa16e409873d0b3b1ade456a3455544eee7 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 4 Oct 2020 18:53:55 +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 --- .../io/legado/app/model/localBook/LocalBook.kt | 16 +++++++++++----- .../ui/association/FileAssociationViewModel.kt | 17 ++++------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 1af6f027e..33efca514 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -6,10 +6,7 @@ import io.legado.app.App import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.BookHelp -import io.legado.app.utils.FileUtils -import io.legado.app.utils.MD5Utils -import io.legado.app.utils.externalFilesDir -import io.legado.app.utils.isContentPath +import io.legado.app.utils.* import java.io.File @@ -34,7 +31,16 @@ object LocalBook { fun importFile(path: String): Book { val fileName = if (path.isContentPath()) { val doc = DocumentFile.fromSingleUri(App.INSTANCE, Uri.parse(path)) - doc?.name ?: "" + doc?.let { + val bookFile = FileUtils.getFile(AnalyzeTxtFile.cacheFolder, it.name!!) + if (!bookFile.exists()) { + bookFile.createNewFile() + doc.readBytes(App.INSTANCE)?.let { bytes -> + bookFile.writeBytes(bytes) + } + } + } + doc?.name!! } else { File(path).name } diff --git a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt index 327016fc6..86c0e52e4 100644 --- a/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt @@ -5,12 +5,12 @@ import android.content.Intent import android.net.Uri import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.MutableLiveData -import io.legado.app.App import io.legado.app.base.BaseViewModel -import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.model.localBook.LocalBook import io.legado.app.ui.book.read.ReadBookActivity -import io.legado.app.utils.* +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) { @@ -46,16 +46,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica } if (scheme.isEmpty()) { val book = if (uri.scheme == "content") { - LocalBook.importFile(uri.toString()).apply { - val bookFile = - FileUtils.getFile(AnalyzeTxtFile.cacheFolder, originName) - if (!bookFile.exists()) { - bookFile.createNewFile() - DocumentUtils.readBytes(App.INSTANCE, uri)?.let { - bookFile.writeBytes(it) - } - } - } + LocalBook.importFile(uri.toString()) } else { LocalBook.importFile(uri.path.toString()) }