Fix crash when download location is invalid

This commit is contained in:
FooIbar committed 2024-09-27 19:57:47 +08:00
1 parent 00d6a2e403
commit c54ac1a4c0
2 files changed
+4 -5

No files matched your search

@@ -64,7 +64,6 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootGraph
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.launchNonCancellable
import eu.kanade.tachiyomi.util.system.logcat
import kotlinx.coroutines.launch
import moe.tarsin.coroutines.runSuspendCatching
@@ -99,10 +98,8 @@ fun AnimatedVisibilityScope.DownloadScreen(navigator: DestinationsNavigator) = c
contentResolver.takePersistableUriPermission(treeUri, FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION)
val path = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)).toOkioPath()
check(path.isDirectory) { "$path is not a directory" }
keepNoMediaFileStatus(path) // Check if the directory is writable
downloadLocationState = path
launchNonCancellable {
keepNoMediaFileStatus(downloadLocationState)
}
}.onFailure {
logcat(it)
launchSnackBar(cannotGetDownloadLocation)
@@ -16,7 +16,9 @@ val Uri.displayPath: String?
val context = appCtx
if (DocumentsContract.isDocumentUri(context, this)) {
val (type, path) = DocumentsContract.getDocumentId(this).split(":", limit = 2)
val (type, path) = DocumentsContract.getDocumentId(this).split(":", limit = 2).also {
if (it.size < 2) return toString()
}
if (authority == "com.android.externalstorage.documents") {
if (type == "primary") {
return Environment.getExternalStorageDirectory().path + "/" + path