簡體   English   中英

使用 Retrofit2 上傳圖像時打開失敗:ENOENT(沒有此類文件或目錄)

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

我一直在嘗試使用 Retrofit2 將在圖庫中選擇的圖像發送到服務器,但它總是給我類似content:/com.android.providers.media.documents/document/image%3A28: open failed:ENOENT (No such file or directory)錯誤content:/com.android.providers.media.documents/document/image%3A28: open failed:ENOENT (No such file or directory) 我認為這是因為路徑不正確,因為當我檢查圖庫時,路徑看起來像這樣/storage/emulated/0/Download/images.png 無論如何我可以獲得正確的路徑嗎? 或者我可以使用我使用URI獲得的路徑獲取圖像嗎? 謝謝。

選擇圖像代碼:

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)
    }
}

上傳代碼:

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

// 使用 FileUtils 通過 uri 獲取實際文件 File file = FileUtils.getFile(this, fileUri);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM