1

I'm trying to save and retrieve image to my web database using JSON but it's seems like I'm not doing it right problem is image is not being reproduce. Here are my codes

For getting the image

BitmapDrawable bitmapDrawable = ((BitmapDrawable)objectImage.getDrawable());

Bitmap b = bitmapDrawable.getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] img = bos.toByteArray();

now after this I'm expecting that the image is now compressed or encrypted in byte[] array now here's how I pass it in my database thru JSON

String imgData = Base64.encodeToString(image, Base64.DEFAULT);

nameValuePairs.add(new BasicNameValuePair("image", imgData));

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(getUri(category));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    httpclient.execute(httppost);
} catch (Exception e) {
   Log.e("log_tag", "Error in http connection " + e.toString());
}

and this is how I'm retrieving it

try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(getUriForRetrieve(category));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();

        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

    } catch (Exception e) {
        Log.e("log_tag", "getProducts()" + e.toString());
    }

    return result;
}

 try {

        JSONArray jArray = new JSONArray(getResult("Letters"));
        JSONObject json_data = null;
        if (jArray.length() > 0) {
            for (int i = 0; i < jArray.length(); i++) {

                json_data = jArray.getJSONObject(i);

                Letters letter = new Letters();

                letter.setImage(Base64.decode(json_data.getString("image"),
                        Base64.DEFAULT));

                letterList.add(letter);
            }
        }

    } catch (JSONException e1) {
        Log.e("log_tag", "getProducts()" + e1.toString());
    } catch (ParseException e1) {
        e1.printStackTrace();
    }

and here's how i decode it to bitmap

Bitmap b1 = BitmapFactory.decodeByteArray(img, 0, img.length);
objectImage.setImageBitmap(b1);

but there's no picture being generated what could be wrong?

thanks in advance

  • I highly discourage you to send images to your server this way. JSON is not meant to contain a potentially enormous length that your `Base64.encodeToString(image, Base64.DEFAULT);` will create. You will also find that many JSON decoders will have a maximum size for the JSON content they will parse. A much better way is to use `MultipartEntityBuilder` and sending your file as a `FileBody` and your other JSON elements as `TextBody` (assuming you're using the apache `HTTPClient`. – kha Mar 06 '15 at 20:29
  • Just to back up my comment with some facts, have a look at this question: http://stackoverflow.com/questions/1262376/is-there-a-limit-on-how-much-json-can-hold and this comment: Based on https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength.aspx `The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.` – kha Mar 06 '15 at 20:34
  • i was able to resolve this but i won't recommend it because of the amount of time before you can retrieve you're image. – edward cullen Apr 19 '15 at 14:01

1 Answers1

1

I had a quite similar issue in this post that I did a couple of days ago and the recommendation they gave me was to use a networking library like Google Volley to make my requests more easier and it's way less work. Also, here is a link for a tutorial that consists of custom ListView with image and text using Volley.

Hope it helps.

Community
  • 1
  • 1
w_lpz
  • 613
  • 4
  • 15
  • 28