Please, help....
I am not sure what is wrong with my code. I am trying to send an image file to MongoDB using Ktor. Server side is ok, I can @POST a file using Postman Software but I can't sand Multipart data from android app.
Uri is ok, I can display selected image on a screen with ActivityResultContracts.GetContent()
Before, I have tried to send an Uri from the screen to ViewModel->Repo->RetrofitApi, now I am trying to send MultipartBody.Part but sadly, the result is the same:
java.io.FileNotFoundException: /document/image:32: open failed: ENOENT (No such file or directory)
This is a method in Screen file (using Jetpack Compose):
PhotoButton(isVerified = true, onClick = {
val file = imageUri!!.path?.let { File(it) }
if (file != null) {
val requestFile: RequestBody =
file.asRequestBody("image/jpg".toMediaTypeOrNull())
multiPartImage = MultipartBody.Part.createFormData("image", file.name, requestFile)
val image = multiPartImage
Log.d("UploadScreen", "Upload: ${file.path} ")
viewModel.uploadPhoto(image = image!!)
} else {
Log.d("UploadScreen", "Cant get file")
}
}, text = "Send to DB")
Repository:
override suspend fun uploadPhoto(image: MultipartBody.Part): ApiResponse {
return try{
ktorApi.uploadPhoto(image)
} catch (e: Exception){
Log.d("UploadRepositoryImpl: ", "{$e}")
ApiResponse(
success = false,
error = e
)
}
}
Retrofit:
@Multipart
@POST("/upload_photo")
suspend fun uploadPhoto(
@Part filePart: MultipartBody.Part?
):ApiResponse
I have every needed permission in th mianifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
android:requestLegacyExternalStorage="true"