Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add an image file into json object?

Tags:

json

android

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

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

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.

like image 788
Chirag Avatar asked Jan 04 '12 11:01

Chirag


People also ask

How do I add files to a JSON file?

Steps for Appending to a JSON File In Python, appending JSON to a file consists of the following steps: Read the JSON in Python dict or list object. Append the JSON to dict (or list ) object by modifying it. Write the updated dict (or list ) object into the original file.

How do I view an image in a JSON file?

To show the text values (such as the name and id) I can use the elements innerHTML property. However, to display the images using the URLs, I have to first create an <img> element (for each record in the JSON file), and assigning the URL as source to the <img> elements, which is then added to a <table> cell.


2 Answers

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 in JSON
  • Binary Data in JSON String. Something better than Base64
  • BSON library for java

Hope this helps.

like image 192
Ovidiu Latcu Avatar answered Sep 28 '22 03:09

Ovidiu Latcu


You're only adding the File object to the JSON object. The File object only contains meta information about the file: Path, name and so on.

You must load the image and read the bytes from it. Then put these bytes into the JSON object.

like image 44
Flo Avatar answered Sep 28 '22 04:09

Flo