[英]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)
.我一直在尝试使用 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)
。 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
.我认为这是因为路径不正确,因为当我检查图库时,路径看起来像这样
/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
?或者我可以使用我使用
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 //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); // 使用 FileUtils 通过 uri 获取实际文件 File file = FileUtils.getFile(this, fileUri);
//Tutorial https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server //教程https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.