Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用okhttp将图像文件从ImageView上载到nodejs服务器(multer)?科特林_Android_Node.js_Kotlin_Multer_Okhttp3 - Fatal编程技术网

Android 如何使用okhttp将图像文件从ImageView上载到nodejs服务器(multer)?科特林

Android 如何使用okhttp将图像文件从ImageView上载到nodejs服务器(multer)?科特林,android,node.js,kotlin,multer,okhttp3,Android,Node.js,Kotlin,Multer,Okhttp3,我已经编写了使用okhttp和kotlin将图像上传到服务器的代码,也就是说,用户通过摄像头拍摄照片,然后在imageView中显示图像。当用户单击“发送”按钮时,我希望imageView中的图像发送到服务器,但我不知道如何将ImageView中的图像更改为可以发送到服务器的文件,请参阅我的代码以了解更多详细信息 这是我的kotlin代码 fun uploadImage(url:String, image:File, imageName:String){ val MEDIA_TYPE_

我已经编写了使用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”以实现上传图像功能?

希望对您有所帮助

    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)
希望这有帮助

    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)