简体   繁体   中英

Open Failed: ENOENT (No such file or directory) when uploading image using Retrofit2

i've been trying to send image that has been selected in gallery to the server using Retrofit2, but it always gave me error like content:/com.android.providers.media.documents/document/image%3A28: open failed:ENOENT (No such file or directory) . I assume it's because the path is incorrect, because when i check to gallery the path looks like this /storage/emulated/0/Download/images.png . Is there anyways i can get the correct path ? or can i get the image using the path i get using URI ? Thanks.

Choose Image Code :

fun submitFirstKm(view: View) {
    Log.d("Path Image", "Path Image : $filepath")
    CustomWarningDialog(this, batteryPercentage.toString(), "12312312.123123", edt_first_km.text.toString(), filepath.toString())
        .showDialog(Constants.WARNING_DIALOG, "first_km").show()
}

fun startFileChooser(view: View) {
    val i = Intent()
    i.type = "image/*"
    i.action = Intent.ACTION_GET_CONTENT
    startActivityForResult(Intent.createChooser(i, "Choose Picture"), REQUEST_CODE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
        filepath = data.data!!
        val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, filepath)
        img_first_km.setImageBitmap(bitmap)
    }
}

Upload Code :

private fun submit(){
    try {
        val file: File = File(uri)
        val requestFile: RequestBody = file.asRequestBody("image/*".toMediaTypeOrNull())
        val filePhoto : MultipartBody.Part = MultipartBody.Part.createFormData("file",file.name,requestFile)
        val firstKm: RequestBody = km.toRequestBody(MultipartBody.FORM)
        val coordinates: RequestBody = coordinate.toRequestBody(MultipartBody.FORM)
        val batteryPercentage: RequestBody = batteryPercentage.toRequestBody(MultipartBody.FORM)

        ApiConfig().getService()
            .firstKm(firstKm, coordinates, batteryPercentage, filePhoto)
            .enqueue(object : Callback<FirstKmResponse> {
                override fun onFailure(call: Call<FirstKmResponse>, t: Throwable) {
                    Toast.makeText(mContext, "Gagal : ${t.message}", Toast.LENGTH_SHORT).show()
                }

                override fun onResponse(
                    call: Call<FirstKmResponse>,
                    response: Response<FirstKmResponse>
                ) {
                    if (response.isSuccessful) {
                        Toast.makeText(mContext, "Berhasil", Toast.LENGTH_SHORT).show()
                    } else {
                        Toast.makeText(mContext, "Gagal : ${response.code()}", Toast.LENGTH_SHORT).show()
                    }
                }
            })
    } catch (e: Exception) {
        Log.e("FirstKmException", "Exception $e")
    }
}

//https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java

// use the FileUtils to get the actual file by uri File file = FileUtils.getFile(this, fileUri);

//Tutorial https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM