I am trying to write logic to upload an image, using Retrofit2 and Jetpack Compose. When creating a File object from the image's Uri, the File object seems to have content://
scheme instead of file://
, which is causing FileNotFoundException
while creating the request object for Retrofit2.
For example, the below code throws IllegalStateException.
fun upload(imageUri: Uri) {
val file = File(imageUri.path!!)
if (!file.exists()) {
throw IllegalStateException("File $imageUri does not exist.")
}
...
}
The exception is as below:
java.lang.IllegalStateException: File content://media/external/images/media/1000000363 does not exist
Note that the Uri is valid, and I'm able to use it to display the image in the UI.
I have tried creating the File
object in different ways, such as imageUri.toFile()
, which throws:
java.lang.IllegalArgumentException: Uri lacks 'file' scheme: content://media/external/images/media/1000000364
Thanks in advance!
I fixed by issue by using InputStream as described in this article