Search code examples
androidjsonmultipartokhttp

How to pass image file inside jsonArray using MultipartBuilder


I am working with multipart builder and I have to pass an image inside jsonArray.

Then, I have to send that created jsonArray as a value inside MultipartBuilder.

I can pass single image to MultipartBuilder as below :

 multipartBuilder.addFormDataPart("image", "im.png", RequestBody.create(MediaType.parse("image/*"), fileImagePath));

Now, my jsonArray is as below which I have created manually:

[{"length":"1.1","item_image":"\bitmaptoprint.jpg","quantity":"1","weight":"11","height":"1.1","item_name":"item1","width":"1.1"},{"length":"2.2","item_image":"/Demo###.jpg","quantity":"2","weight":"2","height":"2.2","item_name":"item2","width":"2.2"}]

Now, I am passing this jsonArray in multipart builder as below :

 .addFormDataPart("array", jsonItemArray.toString());

As From above jsonArray, you can see that i am passing image in 'item_image' parameter. But, unfortunately, images can't upload to server.

Is I am doing anything wrong ? Is there any other way available to do so ?

Please, let me help to solve this issue.


Solution

  • If image small like avatar, you can use Base64 ecoding, ecode image to long string, add in your json it, and send to server only json, and on server side you will need decode 64 string to resolve image.