asdec90
asdec90

Reputation: 1082

How to sent File (Image) along with String parameters in the Body of a POST api (Volley)

Was sending the collection of String Params in the Hashmap in the Api. Now it is required to add a parameter File that has to be an Image.

The body of the POST api looks as below:

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

I have seen many examples of Multipart requests but none solved the issue. Looking for an approach/example.

Upvotes: 0

Views: 544

Answers (1)

NOTE: It's alternative way to sending Image as a File.

You can try converting Image to BASE64 String, and send it as a string.

First, convert your bitmap to byte array:

//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(); 

Then, encode it to BASE64 String:

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

Finally put it to your hashmap as a String.

Upvotes: 0

Related Questions