I am trying to upload a picture to server with retrofit but i get the error:
java.io.FileNotFoundException: /document/image:21269: open failed: ENOENT (No such file or directory)
After i get my picture URI i transform it to File and send it to my function which looks like this:
override suspend fun uploadProfilePicture(userId: Long, file: File): ApiCallResource<Int> {
val part = MultipartBody.Part.createFormData(
name = "image",
filename = file.name,
file.asRequestBody("image/*".toMediaTypeOrNull())
)
val response = userService.postProfilePic(userId, part)
return if(response.code() == 200) {
ApiCallResource.Success(1)
} else {
val errorMessage = response.errorBody()!!.string()
val jObjError = JSONObject(errorMessage)
ApiCallResource.Error(jObjError.getString("message"))
}
}
the post emthod looks like this:
@Multipart
@POST("/{userId}/postUserProfile")
suspend fun postProfilePic(
@Path("userId") userId: Long,
@Part image: MultipartBody.Part
): Response<ResponseBody>
In the manifest i have these permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
I tried opening the URI as InputStream and set the body of the image as ByteArray and it works, but I would like to know why do i keep getting the ("java.io.FileNotFoundException: /document/image:21269: open failed: ENOENT (No such file or directory") exception.
From the URI I am getting the File like this: val file: File = File(it[0].path)