Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By SurfMan
  • 1 Post By SurfMan

Thread: unable to store image as binary via JSON

  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default unable to store image as binary via JSON

    Dear Senior!

    i am writing an android application to store the customer sign, i convert the sign as Bitmap then binary data,
    But while i sent by using JSON it showing some errors..,

    By the blow code i read the bitmap and compress then convert it as Binary data
    Java Code:
    Canvas canvas = new Canvas(mBitmap);
    			try {
    				v.draw(canvas);
    				ByteArrayOutputStream bao = new ByteArrayOutputStream();
    				mBitmap.compress(Bitmap.CompressFormat.PNG, 100, bao);
    				byte[] ing_byt = bao.toByteArray();
    				
    				StringBuilder sb = new StringBuilder();
    			    for (byte by : ing_byt)
    			        sb.append(Integer.toBinaryString(by & 0xFF));
    			    Log.d("ba", sb.toString());
    			    
    				Intent intent = new Intent();
    				Bundle b = new Bundle();
    				b.putString("cust_sign", sb.toString());
    				intent.putExtras(b);
    				setResult(RESULT_OK, intent);
    
    			} catch (Exception e) {
    				Log.v("log_tag", e.toString());
    			}
    This is the code to receive my binary data

    Java Code:
    if (requestCode == 10) {
    			if (null != data) {
    				Bundle bun = data.getExtras();
    				cust_sign = bun.getString("cust_sign");
    				Log.d("CallReport_sign", cust_sign);
    				new ServiceUpdate().execute();
    			}
    		}

    the result is

    Java Code:
    D/SerVarCstImage(1131): 100010011010000100111010001111101101011010101000011011001001100100010001001010010001011010000111001111100010000110010111010100110101111001100011111001110000101001001101010010111010111001110111000110110000000001101110010111100100110001001000001101010011110001001110011101101110111011011011111000110111001001101101101100001110000010110011111110111011001000010011100110000110111101100011101000110000111001111001100100000011011110100101101100101100111001011100000010111111001011011000011111011100110100000011011110100101101100101111011001011110000001111100101100110000111101001011110000011000010110010110011001100110110011101000101100111100110100110010111011011101011110001100111111010111101011011011110110101010010111111111010110101111111110111110001110000101001101100101011100110101011001001111110110111010101010101001110100111011000101000000000000000000000000000000000000000000000000100000001011011111101110110111101000110110000001111101011010001111111100011011100111111010111100000001101101110001000101011110001111011111000011110101011110010100100111000110001101101111101111101000111101000100001111000100011111100011111010000111110111010011111000100110111001000110111100100011111111000100001100010010000011000101110010011110001101101110100001001101000110101000110101001101101110001001011001110001000110111100001111011111110010100001111000110001110110101001110001001100011010100101101011101000111111110100010011111000111111111010011001111000001111001010010011111011011111101001110100010101001110101011011011100010011110011100010011000111101000011001101111001110011011100011010001101111111110110101000111101000111111100011001111110001011111110110110010100111110100111011011111100110000111000110100101111110110111111110111110001110011101000101101110111110111101011111001101101110110001000100111001000101100111001000101101111001000101010111000100010101111100010111010110111111110110110100111100010011001011100010001100111100011000111100001110111111101011111000100011100010001000111100010110011101101111011010100010111000100010000011101000011101110011000000100010011010110110100011010100011010100101000111000100101110111110101011110110110111111000100100111001000110101100100001101101110100100110000001110110011001100101101110101001010111101010001010111110001011000010100110001001110100001010110110001011100010011001001101111001010001110100011010100110100111100011001111110101011110111111110100110101011100010011100011101000011011011100010010000101011011100001110001000110111101000110101000110110101011001110001001001011111101010101101111111111110000111011111110111111011000010011000111000111110110110001110100111111011011010001101000101101011101100010100101000101011101110011111101001101000101110101111010110110001101001101101010000110111010011111010000000110011101011101100010001000110111011111001101010001001101111001111111101111010111000110111010011101111111000001111000101110001000101011110100110100111111110110101001110011111111000111111101001011111101110110011001011010001100100011001001111110111011010111110011110100111101111000111110101011110100111111001110011110000111011011111011100000110010111010101110010010110001011011111000011011011101101100010100100111101101011011100100010010101110000111111111001011000011011001101000110001011100010110111101011001110100001110111001011010000001110100101110100111110100001111111100000100000111101001011000100101111001011111001011010010001101101101110101010101001110011110011010000010111001111100111000101011001000100101001100000110010110101101010001110110010100100011101010011111001011110000111101111001110000010001100101101010110100111100011111111011100110101001110111111000111001001110011101010010010100000111010110101010010010111100110101111011001001110011000110101111010010110101110011101100111100000001010101000011110101100100011100100110110011111011101011111010010111010011111011001110110011110000000100010001110101010110101010111011001111000011110111110110010011010101001110110100111101010111010000111011011111100001011110010101010100010101111001100101111001111110101110011111001001110000011101011001001010101
    But while i store it, it says "the requested URL is too long", in case if i pass the small amount of binary data as static value it is working well..,

    Guide me please..,

    What i have to do to solve the problems..,

    Thank You!

    Have A Happy Day..,
    - Raj

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: unable to store image as binary via JSON

    The URL is too long, means the URL is too long. Doesn't get more exciting than this. I assume you are making a GET request, which indeed has limits to the size. Try making a POST request instead.
    gimbal2 likes this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: unable to store image as binary via JSON

    Why on earth are you turning an image into a JSON string?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: unable to store image as binary via JSON

    JSON string? Its actually binary in String form. I find it a highly entertaining (but very inefficient) way of solving this requirement :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: unable to store image as binary via JSON

    All your base64 are belong to us!
    gimbal2 likes this.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: unable to store image as binary via JSON

    Quote Originally Posted by gimbal2 View Post
    JSON string? Its actually binary in String form. I find it a highly entertaining (but very inefficient) way of solving this requirement :)
    I couldn't be bothered to read the code.
    Just going on their (vague) description.

    I'm not even sure where the exception's coming from in that lot...I'm assuming it's some code we can't even see.
    Anyway, you can stick byte[]'s into Bundles if you have to.
    And that self same array should be an attachment to the http (I presume) call.

    Unless there's something odd with android, of course.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    Sure Mr.SurfMan

    Thank You for your guide!

    I will try right now..,
    Thank You..,
    - Raj

  8. #8
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    Dear Mr.Tolls!

    I am using JSON parsing to store the image in my server.., the Data Base is SQLServer and i am using PHP for communication sir..,
    - Raj

  9. #9
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    Thank You!

    When i using the "POST" method it is working. But with different error,

    The "GET" method is working well. But Can you please conform the blow code for "POST" is right ?..,

    The Error is
    09-21 12:48:30.431: D/Error_While_Saving(1014): End of input at character 1 of
    Java Code:
    
    public class JSONParser {
    
    	static InputStream is = null;
    	static JSONObject jObj = null;
    	static String json = "";
    
    	public JSONParser() {
    
    	}
    	public String makeHttpRequest(String url, String method,List<NameValuePair> params) {
    		try {
    			if (method == "POST") {
    				DefaultHttpClient httpClient = new DefaultHttpClient();
    				HttpPost httpPost = new HttpPost(url);
    				httpPost.setEntity(new UrlEncodedFormEntity(params));
    				
    				httpPost.setHeader("Accept", "application/json");
    		        httpPost.setHeader("Content-type", "application/json");
    				
    				HttpResponse httpResponse = httpClient.execute(httpPost);
    				HttpEntity httpEntity = httpResponse.getEntity();
    				is = httpEntity.getContent();
    
    			} else if (method == "GET") {
    				DefaultHttpClient httpClient = new DefaultHttpClient();
    				String paramString = URLEncodedUtils.format(params, "utf-8");
    				url += "?" + paramString;
    				HttpGet httpGet = new HttpGet(url);
    
    				HttpResponse httpResponse = httpClient.execute(httpGet);
    				HttpEntity httpEntity = httpResponse.getEntity();
    				is = httpEntity.getContent();
    			}
    
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		} catch (ClientProtocolException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(
    					is, "iso-8859-1"), 8);
    			StringBuilder sb = new StringBuilder();
    			String line = null;
    			while ((line = reader.readLine()) != null) {
    				sb.append(line + "\n");
    			}
    			is.close();
    			json = sb.toString();
    		} catch (Exception e) {
    			Log.e("Buffer Error", "Error converting result " + e.toString());
    		}
    		return json;
    	}

    This is the mapping code

    Java Code:
    JSONParser jsonParser2 = new JSONParser();
    
    List<NameValuePair> param = new ArrayList<NameValuePair>();
    param.add(new BasicNameValuePair("SerVarCstImage", cust_sign));
    
    String json = jsonParser2.makeHttpRequest(URL_IMAGE, "POST", param);
    			Log.d("Tag_result", json);
    
    			try {
    				JSONObject success = new JSONObject(json);
    				if (success != null) {
    					String s_message = success.getString("result");
    }
    }
    I am waiting for your replay..,

    Thank You!

    Have A Happy Day..,
    - Raj

  10. #10
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    Can Any One Guide Me Please..,
    Quote Originally Posted by raj.mscking@gmail.com View Post
    Thank You!

    When i using the "POST" method it is working. But with different error,

    The "GET" method is working well. But Can you please conform the blow code for "POST" is right ?..,

    The Error is

    Java Code:
    
    public class JSONParser {
    
    	static InputStream is = null;
    	static JSONObject jObj = null;
    	static String json = "";
    
    	public JSONParser() {
    
    	}
    	public String makeHttpRequest(String url, String method,List<NameValuePair> params) {
    		try {
    			if (method == "POST") {
    				DefaultHttpClient httpClient = new DefaultHttpClient();
    				HttpPost httpPost = new HttpPost(url);
    				httpPost.setEntity(new UrlEncodedFormEntity(params));
    				
    				httpPost.setHeader("Accept", "application/json");
    		        httpPost.setHeader("Content-type", "application/json");
    				
    				HttpResponse httpResponse = httpClient.execute(httpPost);
    				HttpEntity httpEntity = httpResponse.getEntity();
    				is = httpEntity.getContent();
    
    			} else if (method == "GET") {
    				DefaultHttpClient httpClient = new DefaultHttpClient();
    				String paramString = URLEncodedUtils.format(params, "utf-8");
    				url += "?" + paramString;
    				HttpGet httpGet = new HttpGet(url);
    
    				HttpResponse httpResponse = httpClient.execute(httpGet);
    				HttpEntity httpEntity = httpResponse.getEntity();
    				is = httpEntity.getContent();
    			}
    
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		} catch (ClientProtocolException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    		try {
    			BufferedReader reader = new BufferedReader(new InputStreamReader(
    					is, "iso-8859-1"), 8);
    			StringBuilder sb = new StringBuilder();
    			String line = null;
    			while ((line = reader.readLine()) != null) {
    				sb.append(line + "\n");
    			}
    			is.close();
    			json = sb.toString();
    		} catch (Exception e) {
    			Log.e("Buffer Error", "Error converting result " + e.toString());
    		}
    		return json;
    	}

    This is the mapping code

    Java Code:
    JSONParser jsonParser2 = new JSONParser();
    
    List<NameValuePair> param = new ArrayList<NameValuePair>();
    param.add(new BasicNameValuePair("SerVarCstImage", cust_sign));
    
    String json = jsonParser2.makeHttpRequest(URL_IMAGE, "POST", param);
    			Log.d("Tag_result", json);
    
    			try {
    				JSONObject success = new JSONObject(json);
    				if (success != null) {
    					String s_message = success.getString("result");
    }
    }
    I am waiting for your replay..,

    Thank You!

    Have A Happy Day..,
    - Raj

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: unable to store image as binary via JSON

    You do not store binary data in a database as a String.
    For SQL Server you have either varbinary(max) which gives you up to 2Mb of data, or FILESTREAM which is essentially a BLOB.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    Thank You Mr.Tolls!

    I will try as your idea,

    But my problem is unable to receive any response from my server side, the my progress bar will exit only after getting any value (Success/Failure) from the server. But it progressing only sir,

    i tried to the image for as
    1) Byte Array
    2) Binary
    &
    3) Base64 String also

    But when i sending the image by using "GET" it say "Too long string"
    else if i use "POST" as Mr.SurfMan idea it was shown the error message from the server side ..,

    Can you pleas give me a way to solve this problem sir..,

    Thank You!
    - Raj

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: unable to store image as binary via JSON

    It's a file.
    You need to send it as an attachment, which means looking into exactly how to do that in code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    Thank You Mr.Tolls!

    i will try to send as a file sir..,

    Thank You..,
    - Raj

  15. #15
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    I need Your Help Please!

    I have been try to save the image with some user data to server by using JSON & Http POST method..,

    i am facing the problems only..,

    which one is best to store the image
    1) upload as file
    2) send as binary/string etc..,


    can any one give me some real examples..,

    i confused..,
    Guide me please..,

    Thank You..,
    - Raj

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: unable to store image as binary via JSON

    What framework are you using to do the http connection?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    284
    Rep Power
    3

    Default Re: unable to store image as binary via JSON

    i am using JSON & php for server side coding Mr.Tolls..,
    - Raj

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: unable to store image as binary via JSON

    But what framework/API are you using to make the HTTP connection TO your server, FROM your Android application?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Binary Search / Sort - Unable To Retrieve Index
    By sequential in forum New To Java
    Replies: 4
    Last Post: 08-18-2013, 08:52 PM
  2. Replies: 4
    Last Post: 04-26-2012, 07:05 PM
  3. Unable to display gif image
    By rameshiit19 in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 05-09-2011, 10:43 AM
  4. Unable to set image as background
    By MrPremium in forum New To Java
    Replies: 1
    Last Post: 04-01-2011, 01:00 AM
  5. image store process
    By sandeep23k in forum New To Java
    Replies: 4
    Last Post: 08-14-2010, 06:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •