我如何添加一个图像文件转换成JSON对象?图像、对象、文件转换成、JSON

2023-09-12 23:42:29 作者:枕边的月光

我想一个图像文件添加到JSON对象。是否有可能将图像文件添加到JSON对象?

I want to add an image file into json object . Is it possible to add image file into json object?

我试过低于code,但它不工作?因为我要的JSON对象发送到服务器,然后服务器会读我的图像文件并存储到该数据库中。

I tried below code but its not working ? Because i want to send that json object to the server then server will read my image file and store into that database.

JSONObject test = new JSONObject();
test.put("photo",new File(// Here i set image uri));

所以,当我打印此JSON对象,它只是告诉我那里的图像存储图像路径。我希望文件将数据发送到服务器。

So when I print this json object it only show me the image path where the image stored. I want file for sending it to server.

推荐答案

您将需要阅读字节文件字节[] ,并把该对象插入的JSONObject

You will need to read the bytes from that File into a byte[] and put that object into your JSONObject.

您也应该看看下面的文章:

You should also have a look at the following posts :

的ByteArray以JSON Binary在JSON字符串数据。比的Base64 更好的东西 BSON库的Java ByteArray in JSON Binary Data in JSON String. Something better than Base64 BSON library for java

希望这有助于。