[英]how to upload image file from ImageView to nodejs server (multer) using okhttp? Kotlin
我已经编写了使用okhttp和kotlin将图像上传到服务器的代码,也就是说,用户通过相机拍照,然后当用户点击发送按钮时在imageView中显示图像,我想要从ImageView发送图像到服务器,但我不知道如何从ImageView更改图像是一个可以发送到服务器的文件,请参阅我的代码了解更多详情
这是我的kotlin代码
fun uploadImage(url:String, image:File, imageName:String){
val MEDIA_TYPE_PNG = MediaType.parse("image/png")
val client = OkHttpClient()
val requestBody = MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", imageName, RequestBody.create(MEDIA_TYPE_PNG, image))
.build()
val request = Request.Builder()
.url(url)
.post(requestBody)
.build()
client.newCall(request).enqueue(object:Callback{
override fun onFailure(call: Call, e: IOException) {
}
override fun onResponse(call: Call, response: Response) {
Log.i(TAG,"response ${response.body?.string()}")
}
})
}
问题是,如何将imageView中的图像转换为“image:File”for uploadImage函数?
希望这有帮助
val url = getString(R.string.urlUpload)
val MEDIA_TYPE_JPEG = MediaType.parse("image/jpeg")
val bitmap = (img_register.drawable as BitmapDrawable).bitmap
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val bitmapByteArray = baos.toByteArray()
val file = Base64.encodeToString(bitmapByteArray,Base64.DEFAULT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.