diff --git a/app/src/main/java/io/legado/app/ui/book/import/local/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/import/local/ImportBookActivity.kt index 2eafcce82..c00ca0ecb 100644 --- a/app/src/main/java/io/legado/app/ui/book/import/local/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/import/local/ImportBookActivity.kt @@ -46,22 +46,19 @@ class ImportBookActivity : BaseImportBookActivity - if (uri.isContentScheme()) { - AppConfig.importBookPath = uri.toString() - initRootDoc(true) - } else { - AppConfig.importBookPath = uri.path - initRootDoc(true) - } + AppConfig.importBookPath = uri.toString() + initRootDoc(true) } } override fun onActivityCreated(savedInstanceState: Bundle?) { binding.titleBar.setTitle(R.string.book_local) launch { - setBookStorage() initView() initEvent() + if (setBookStorage() && AppConfig.importBookPath.isNullOrBlank()) { + AppConfig.importBookPath = AppConfig.defaultBookTreeUri + } initData() } } @@ -150,35 +147,43 @@ class ImportBookActivity : BaseImportBookActivity upPath() - lastPath.isNullOrEmpty() -> { + if (viewModel.rootDoc != null && !changedFolder) { + upPath() + } else { + val lastPath = AppConfig.importBookPath + if (lastPath.isNullOrBlank()) { binding.tvEmptyMsg.visible() selectFolder.launch() - } - lastPath.isContentScheme() -> { - val rootUri = Uri.parse(lastPath) - kotlin.runCatching { - val doc = DocumentFile.fromTreeUri(this, rootUri) - if (doc == null || doc.name.isNullOrEmpty()) { + } else { + val rootUri = if (lastPath.isUri()) { + Uri.parse(lastPath) + } else { + Uri.fromFile(File(lastPath)) + } + when { + rootUri.isContentScheme() -> { + kotlin.runCatching { + val doc = DocumentFile.fromTreeUri(this, rootUri) + if (doc == null || doc.name.isNullOrEmpty()) { + binding.tvEmptyMsg.visible() + selectFolder.launch() + } else { + viewModel.subDocs.clear() + viewModel.rootDoc = FileDoc.fromDocumentFile(doc) + upDocs(viewModel.rootDoc!!) + } + }.onFailure { + binding.tvEmptyMsg.visible() + selectFolder.launch() + } + } + Build.VERSION.SDK_INT > Build.VERSION_CODES.Q -> { binding.tvEmptyMsg.visible() selectFolder.launch() - } else { - viewModel.subDocs.clear() - viewModel.rootDoc = FileDoc.fromDocumentFile(doc) - upDocs(viewModel.rootDoc!!) } - }.onFailure { - binding.tvEmptyMsg.visible() - selectFolder.launch() + else -> initRootPath(rootUri.path!!) } } - Build.VERSION.SDK_INT > Build.VERSION_CODES.Q -> { - binding.tvEmptyMsg.visible() - selectFolder.launch() - } - else -> initRootPath(lastPath) } } diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 1228907b5..f4f941047 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -17,9 +17,7 @@ fun String?.isContentScheme(): Boolean = this?.startsWith("content://") == true fun String.toEditable(): Editable = Editable.Factory.getInstance().newEditable(this) fun String.parseToUri(): Uri { - return if (isContentScheme()) { - Uri.parse(this) - } else { + return if (isUri()) Uri.parse(this) else { Uri.fromFile(File(this)) } }