mirror of
https://github.com/gedoor/legado.git
synced 2024-09-03 09:55:00 +08:00
Merge pull request #2398 from Xwite/master
feat(LocalBook): `Book.getLocalUri` return dynamic value
This commit is contained in:
commit
7024a35635
@ -1,13 +1,17 @@
|
||||
package io.legado.app.help.book
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.constant.BookSourceType
|
||||
import io.legado.app.constant.BookType
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.BookSource
|
||||
import io.legado.app.help.config.AppConfig.defaultBookTreeUri
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import io.legado.app.utils.getFile
|
||||
import java.io.File
|
||||
import splitties.init.appCtx
|
||||
|
||||
|
||||
val Book.isAudio: Boolean
|
||||
@ -50,10 +54,25 @@ val Book.isOnLineTxt: Boolean
|
||||
|
||||
fun Book.getLocalUri(): Uri {
|
||||
if (isLocal) {
|
||||
if (bookUrl.isContentScheme()) {
|
||||
return Uri.parse(bookUrl)
|
||||
val originBookUri = if (bookUrl.isContentScheme()) {
|
||||
Uri.parse(bookUrl)
|
||||
} else {
|
||||
Uri.fromFile(File(bookUrl))
|
||||
}
|
||||
//不同的设备书籍保存路径可能不一样 优先尝试寻找当前保存路径下的文件
|
||||
defaultBookTreeUri ?: return originBookUri
|
||||
val treeUri = Uri.parse(defaultBookTreeUri)
|
||||
return if (treeUri.isContentScheme()) {
|
||||
DocumentFile.fromTreeUri(appCtx, treeUri)?.run {
|
||||
findFile(originName)?.let {
|
||||
if (it.exists()) it.uri else originBookUri
|
||||
} ?: originBookUri
|
||||
} ?: originBookUri
|
||||
} else {
|
||||
val treeFile = File(treeUri.path!!)
|
||||
val file = treeFile.getFile(originName)
|
||||
if (file.exists()) Uri.fromFile(file) else originBookUri
|
||||
}
|
||||
return Uri.fromFile(File(bookUrl))
|
||||
}
|
||||
throw NoStackTraceException("不是本地书籍")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user