Skip to content

Commit a93fd6b

Browse files
committed
Fixed file not found exception
1 parent c763f7b commit a93fd6b

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

JetPDFVue/src/main/java/com/pratikk/jetpdfvue/state/VueFilePicker.kt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.core.net.toUri
2424
import com.pratikk.jetpdfvue.util.getDateddMMyyyyHHmm
2525
import com.pratikk.jetpdfvue.util.getFileType
2626
import com.pratikk.jetpdfvue.util.toFile
27+
import kotlinx.coroutines.CancellationException
2728
import kotlinx.coroutines.CoroutineStart
2829
import kotlinx.coroutines.Dispatchers
2930
import kotlinx.coroutines.Job
@@ -134,26 +135,33 @@ class VueFilePicker {
134135
importJob = launch(context = coroutineContext + Dispatchers.IO, start = CoroutineStart.LAZY) {
135136
with((vueFilePickerState as VueFilePickerState.VueFilePickerImported)) {
136137
//Create a temp file using result uri
137-
val file = context.contentResolver.openInputStream(uri)?.use {
138-
val ext = when (uri.getFileType(context)) {
139-
VueFileType.PDF -> {
140-
"pdf"
141-
}
138+
val file = try {
139+
context.contentResolver.openInputStream(uri)?.use {
140+
val ext = when (uri.getFileType(context)) {
141+
VueFileType.PDF -> {
142+
"pdf"
143+
}
142144

143-
VueFileType.IMAGE -> {
144-
"jpg"
145-
}
145+
VueFileType.IMAGE -> {
146+
"jpg"
147+
}
146148

147-
VueFileType.BASE64 -> {
148-
"txt"
149+
VueFileType.BASE64 -> {
150+
"txt"
151+
}
149152
}
153+
it.toFile(ext)
154+
}
155+
} catch (e: CancellationException) {
156+
throw e
157+
} catch (_: Exception) {
158+
null
159+
}
160+
file?.let {
161+
interceptResult(it)
162+
if (isActive) {
163+
onResult(file)
150164
}
151-
it.toFile(ext)
152-
}!!
153-
interceptResult(file)
154-
155-
if (isActive) {
156-
onResult(file)
157165
}
158166
}
159167
}.apply {

0 commit comments

Comments
 (0)