pull/416/head
gedoor 4 years ago
parent 4148ed0ba5
commit e64a8aa16e
  1. 16
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  2. 17
      app/src/main/java/io/legado/app/ui/association/FileAssociationViewModel.kt

@ -6,10 +6,7 @@ import io.legado.app.App
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.utils.FileUtils import io.legado.app.utils.*
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.externalFilesDir
import io.legado.app.utils.isContentPath
import java.io.File import java.io.File
@ -34,7 +31,16 @@ object LocalBook {
fun importFile(path: String): Book { fun importFile(path: String): Book {
val fileName = if (path.isContentPath()) { val fileName = if (path.isContentPath()) {
val doc = DocumentFile.fromSingleUri(App.INSTANCE, Uri.parse(path)) 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 { } else {
File(path).name File(path).name
} }

@ -5,12 +5,12 @@ import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.ui.book.read.ReadBookActivity 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 import java.io.File
class FileAssociationViewModel(application: Application) : BaseViewModel(application) { class FileAssociationViewModel(application: Application) : BaseViewModel(application) {
@ -46,16 +46,7 @@ class FileAssociationViewModel(application: Application) : BaseViewModel(applica
} }
if (scheme.isEmpty()) { if (scheme.isEmpty()) {
val book = if (uri.scheme == "content") { val book = if (uri.scheme == "content") {
LocalBook.importFile(uri.toString()).apply { LocalBook.importFile(uri.toString())
val bookFile =
FileUtils.getFile(AnalyzeTxtFile.cacheFolder, originName)
if (!bookFile.exists()) {
bookFile.createNewFile()
DocumentUtils.readBytes(App.INSTANCE, uri)?.let {
bookFile.writeBytes(it)
}
}
}
} else { } else {
LocalBook.importFile(uri.path.toString()) LocalBook.importFile(uri.path.toString())
} }

Loading…
Cancel
Save