edward cullen
edward cullen

Reputation: 37

How to save and retrieve image in a web database thru JSON in ANDROID

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

Upvotes: 1

Views: 583

Answers (1)

w_lpz
w_lpz

Reputation: 613

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.

Upvotes: 1

Related Questions