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()) }