我们如何将图像发送到Kotlin的服务器?

发布于 2025-02-13 01:47:35 字数 417 浏览 0 评论 0 原文

我尝试将我从当地画廊获得的图像发送到大招牌。图像所需的类型是字符串($二进制)。如何将其发送到服务器?我已经在Google和YouTube中进行了搜索,但是看来我仍然没有找到所需的教程,有时它比碎片在活动中。所以我感到困惑。我是科特林的新手。这是我的片段代码: https://codeshare.io/3amwy6 ,这是我的ViewModel代码: https://codeshare.io/lw0xnl 谢谢您的回答。

I try yo send the image that i get from local gallery to swagger. The required type for image is string ($binary). How can i send it to server? I've search in google and youtube but it seems i still doesn't find the tutorial i need, sometimes it is in activity than fragment. So i confused. I'm new in kotlin. This is my fragment code: https://codeshare.io/3AMWY6 and this is my viewModel code: https://codeshare.io/Lw0xnl Thank you for answering..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千寻… 2025-02-20 01:47:36

推荐方法

如果将图像上传到服务器。您可以选择Multipartbody的类型。部分并进行改造,您可以在此教程简化编码中找到解决方案
android上传


文件

可以将图像转换为由位组成的位图,然后用此 library 由PixelCarrot撰写,或者您可以手动转换它

private fun btmpToString(){
     val outputStream= ByteArrayOutputStream()
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
     val byteArray = outputStream.toByteArray()
     return Base64.encodeToString(byteArray , Base64.DEFAULT)
}

Recomended Method

If you are uploading image to server. You can choose type of MultipartBody.Part and sending with retrofit you can find your solution in this tutorial Simplified Coding
Android Upload File to Server


Requested Method

Firstly you can convert image to bitmap which is consist of bits then convert to string with this library written by pixelcarrot or you can convert it manually

private fun btmpToString(){
     val outputStream= ByteArrayOutputStream()
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
     val byteArray = outputStream.toByteArray()
     return Base64.encodeToString(byteArray , Base64.DEFAULT)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文