如何将Image转换为byteArray



我有一个android应用程序,我使用这种方法转换从相机拍摄的照片

public fun imageToBitmap(image: Bitmap): ByteArray 
{
val stream = ByteArrayOutputStream()
image.compress(Bitmap.CompressFormat.PNG, 90, stream)
return stream.toByteArray()
}

但是当我试图上传我转换的照片"byteArray"到API时,我得到了这个错误

{
"errors": {
"": [
"Unexpected end when deserializing object. Path '', line 1, position 855."
],
"photo_.pphoto": [
"The supplied value is invalid."
]

我正在使用asp。带有SQL服务器数据库的Net API,数据库中的照片类型为varBinary(Max(-上传到API成功预先转换的图像样本(使用此网站https://codebeautify.org/base64-to-image-converter";但它不适用于使用我的方法的照片转换器。我错过了什么?

您需要对byte[]数据进行Base64编码。

private fun getBase64String(array: ByteArray) = Base64.encodeToString(array, Base64.DEFAULT)

调用代码:

getBase64String(imageToBitmap(..))

最新更新