0

I already have a Camera app in android but now I want to save the image to Mysql. Would it be possible to transform the Image to a JsonObject then a String. Then, send the string thru Http Post. Then transform the String to JSON then File then InputStream. Then, save the inputstream to mysql. I know it's kind of cloudy as of this moment but it's just an idea. Wanted to ask if it is possible

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Claudia
  • 11
  • 6

2 Answers2

0

Yes it is possible.

First Convert your image to Base64.

How to convert a image into Base64 string?

Then then send it through HttpPost.

How to send POST request in JSON using HTTPClient?

In the server you need to convert the Base64 string to an image and write it to a physical location on the server. Store the path of the image in the database.

Convert Base64 string to an image file?

I dont think you need the GSON library unless you need to download data from the server.

Community
  • 1
  • 1
K Neeraj Lal
  • 6,560
  • 3
  • 21
  • 32
  • I just used the other answer, I havent tried this one though. sorry! thanks for the response though :) – Claudia Jun 20 '15 at 11:31
0

Actually there is a small change in what you want, that is first convert image to string and then to JSON object. This may be a long process, but i hope it will help you.

If you are taking image from resources then follow this,

Bitmap bitmapImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageStr = Base64.encodeToString(b,Base64.DEFAULT);

Now image is converted to string. Then convert string to JSON and send through HTTP Post.

HttpClient client=new DefaultHttpClient();
String url="Your url";
HttpPost httpPost=new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(buildRequest(imageStr)));


public String buildRequest(String imageStr){
 String jsonText="";
 try{
   JSONObject obj = new JSONObject();
   obj.put("imageStr", imageStr);
   jsonText=JSONValue.toJSONString(obj);
  }
  catch(Exception e){
     e.printStackTrace();
  }
  return jsonText;
}

This is code in transmitting side. I used json-simple-1.1.1.jar. Let me know whether it worked or not. If you have any doubts or want code in MySql side also please ask me.

For server side, I used jersey RestFul services. It helped me a lot because i am using Mac OS. Ok leave it, I think following code may help u to get the request as JSON OBJECT.

String jsonStr="";
while(scan.hasNext()){
    jsonStr+=scan.nextLine();
}
scan.close();
JSONObject obj=new JSONObject(jsonStr);
String imageStr=obj.getString("imageStr");
byte[] imageByteArray=Base64.decode(imageStr);

Once you get the image as bytes u can directly insert it into mysql. Note the type of column should be BLOB type.Use following code:

PreparedStatement pre = conn.prepareStatement("insert into TABLENAME values(?)");
pre.setBytes(1, imageByteArray);
records=pre.executeUpdate();

I think it will work. But let me know what error r u getting. Actually same thing I have implemented successfully using Jersey framework. But I hope u can do without that. Thank you.

  • Thank you for your help :) Ill try this then ill comment again – Claudia Jun 19 '15 at 07:23
  • Hello, how do I transform the JSONObject to Inputstream in doPost? :) – Claudia Jun 20 '15 at 11:09
  • Would it be correct to use JSONObject obj = new JSONObject(result)? or String s = request.getParameter(request) then JSONObject obj = new JSONObject(s)? – Claudia Jun 20 '15 at 11:30
  • Hello I tried using your soluton but then I tried it I got an error -- 06-21 00:50:23.150: E/AndroidRuntime(479): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.json.simple.JSONValue" on path: DexPathList[[zip file "/data/app/com.example.cameraapp-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] – Claudia Jun 20 '15 at 16:50
  • I mentioned already in last sentence that i used json-simple-1.1.1.jar. Download it and add that jar to libs folder. If not worked add that library to java build path of the project also. To go to java build path, Right click on project and select properties. In properties u can see java build path. In that click on Add External jars and select the json-simple-1.1.1.jar. – srikanth kumar Jun 21 '15 at 10:05
  • For server side first I recommend u to download following jar files and add to libs and java build path. Jar files are: json-simple-1.1.1.jar, org-apache-commons-codec.jar, sun.misc.Base64Decoder .jar. – srikanth kumar Jun 21 '15 at 10:09