Has anyone here experience with uploading images/files to the server using Retrofit or OkHttp?
I’m wondering what is the best solution for that in 2022 using Kotlin.
There are a lot answers on that in the internet but most of them are copying the content from the Uri to a File and then use this file to create the requestBody.
But then there are also people like blackapps and commonware who commend under a lot of stackoverflow questions that it’s not recommended to work with Files when there is a Uri that can do everything.
Here commonware explained how to create an InputStream RequestBody using the Uri in Kotlin:
https://commonsware.com/blog/2020/07/05/multipart-upload-okttp-uri.html
unfortunately this is not working for me:
https://stackoverflow.com/questions/72307214/upload-image-from-uri-to-retrofit-s3-signed-url-not-working
I really would love to know what you guys would do? What is the best approach in 2022 using Kotlin (and Jetpack compose)? Is copying the file to the internal storage and deleting it after upload still a good approach? It’s really difficult to find answers to that on Google, most solutions are written in Java and probably outdated.
I also saw the approach of just calling .toFile() on that Uri but this seems to throw an error
Error: java.lang.IllegalArgumentException: Uri lacks 'file' scheme: <content://media/external/images/media/54545>