0

I am trying to post an Image into the server, I manage to get the Image URI and Bitmap and want to transform it into a File so I can post it using Retrofit. but I get this error

FileNotFoundException: /document/image:29467: open failed: ENOENT (No such file or directory)

That is the Code I am using to get he URI

   var imageUri by remember { mutableStateOf<Uri?>(null) }
val context = LocalContext.current
val bitmap = remember { mutableStateOf<Bitmap?>(null) }
imageUri?.let {
        if (Build.VERSION.SDK_INT < 28) {
            bitmap.value = MediaStore.Images
                .Media.getBitmap(context.contentResolver, it)
        } else {
            val source = ImageDecoder.createSource(context.contentResolver, it)
            bitmap.value = ImageDecoder.decodeBitmap(source)
        }

    }

            val file = File(imageUri!!.path)

and This is how I transform it into a request body in my Viewmodel

   val response = serviece.addYourBrand(name,
                image = MultipartBody.Part.createFormData("image", file.name, file.asRequestBody()),
                categoryIDFormBody).body()

and that's my Service Interface

   @Multipart
    @POST("brand")
    suspend fun addYourBrand (
        @Part("name") name : RequestBody,
        @Part image : MultipartBody.Part,
        @Part("category_id[]") categoryId : RequestBody
    ) : Response<BaseResponse>
Ahmed Rabee
  • 185
  • 14
  • 1
    You must convert the Uri to a File. Check out [this answer](https://stackoverflow.com/a/68792304/15763443) – Unes Aug 09 '22 at 17:15
  • No need trying to get a file path. Retrofit can handle the uri. – blackapps Aug 10 '22 at 04:16
  • I found the answer in @Nesyou comment. It's here [Convert file: Uri to File in Android](https://stackoverflow.com/questions/2975197/convert-file-uri-to-file-in-android/68792304#68792304) – Ahmed Rabee Aug 10 '22 at 15:21

0 Answers0