【问题标题】:How to sent File (Image) along with String parameters in the Body of a POST api (Volley)如何在 POST api(Volley)的正文中发送文件(图像)和字符串参数
【发布时间】:2016-04-03 04:19:30
【问题描述】:

在 Api 的 Hashmap 中发送字符串参数的集合。现在需要添加一个必须是图像的参数文件。

POST api 的主体如下所示:

Key1, Value1, Text
Key2, Value2, Text
Key3, Value3, File

我见过很多多部分请求的例子,但没有一个能解决问题。 寻找方法/示例。

【问题讨论】:

  • 服务器期望什么?你也控制服务器吗?

标签: android android-volley


【解决方案1】:

注意:这是将图像作为文件发送的另一种方法。

您可以尝试将 Image 转换为 BASE64 字符串,并将其作为字符串发送。

首先,将您的位图转换为字节数组:

//can use lower value than 100 for more compression or change compression format as JPEG

ByteArrayOutputStream bAOS = new ByteArrayOutputStream();  
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bAOS);
byte[] byteArray = bAOS.toByteArray(); 

然后,将其编码为 BASE64 字符串:

String encodedString = Base64.encodeToString(byteArray, Base64.DEFAULT);

最后把它作为一个字符串放到你的 hashmap 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 2017-11-14
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多