2

How to retrieve the image_url from json to android.json code is here. I just want to get the image to a button.

{
    "page_menu": {
        "flag": "M"
    },
    "menu": [
        {
            "pid": "0",
            "name": "Home",
            "refid": "1",
            "image_url": "aG9tZS5wbmc=",
            "ord_field": "1"
        },
        {
            "pid": "0",
            "name": "About Us",
            "refid": "2",
            "image_url": "YWJvdXQucG5n",
            "ord_field": "2"
        },
        {
            "pid": "0",
            "name": "Services",
            "refid": "3",
            "image_url": "c2VydmljZTIucG5n",
            "ord_field": "3"
        },
        {
            "pid": "0",
            "name": "Products",
            "refid": "4",
            "image_url": "cHJvZHVjdHMucG5n",
            "ord_field": "4"
        }
    ]
}

here is my code for retrieve the json values, i did any mistakes ?? now also am not getting image

JSONArray jsonMainNode1 = jsonResponse.getJSONArray("menu");
              int lengthJsonArr = jsonMainNode1.length(); 
               for(int i=0; i <lengthJsonArr; i++)
              {                                     
                  JSONObject jsonChildNode = jsonMainNode1.getJSONObject(i);

                      String Pid      = jsonChildNode.optString("pid".toString());
                      String Name     = jsonChildNode.optString("name").toString();
                      String Refid=jsonChildNode.optString("refid".toString());

                      String image     = jsonChildNode.getString("image_url");

                        byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
                  Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);

                ImageView ima=new ImageView(getApplicationContext());
              ima.setImageBitmap(decodedByte);

                      OutputData = Name+ima;  

result is Home android.widget.imageview{4176f0c8VED..ID 0,0-0,0} like these..

my php code is here..

if(mysql_num_rows($result))
{
    $json['page_menu']['flag'] = 'M';
    while($row=mysql_fetch_assoc($result))
    {
        $json['menu'][$i]['pid']=ucwords($row['pid']);
        $json['menu'][$i]['name']=$row['name'];
        $json['menu'][$i]['refid']=$row['refid'];
        $json['menu'][$i]['image_url']=base64_encode($row['image_url']);
        $json['menu'][$i++]['ord_field']=$row['ord_field'];

    }
5
  • Do you able to parse the response? Commented Feb 1, 2014 at 6:31
  • yes, i am getting the string only in my button not image Commented Feb 1, 2014 at 6:33
  • String means? Hv you getting the value for attribute image_url in your object? and if yes then is it base64 or imageUrl? Commented Feb 1, 2014 at 6:44
  • am getting the value base 64. Commented Feb 1, 2014 at 6:46
  • @Rashmi i tried and i posted my code.please check. Commented Feb 1, 2014 at 7:05

3 Answers 3

3

I am assuming you able to parse the json and u got the value of image_url attribute from your json in base 64 format

Convert your base 64 string to image as

byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, ecodedString.length);

and set it to imageView as

image.setImageBitmap(decodedByte);
Sign up to request clarification or add additional context in comments.

1 Comment

You cannot directly give image url in decodeByteArray(); First parameter should be the byte array of image not byte array of URL. By the way I am not the person who down voted :)
0
JSONObject obj = new JSONObject(respponse);

JSONArray arr = obj.GetJSONArray("menu");

JSONObject data = arr.GetJSONObject(0);

String image_url = data.GetString("image_url");

Comments

0

You cannot directly give image url in decodeByteArray(); First parameter should be the byte array of image not byte array of URL.

Please check the documentation. http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray(byte[], int, int)

Check the below links to load image from url Android load from URL to Bitmap Android Drawable Images from URL

1 Comment

i didnt use any url, image_url is my table column name,i stored my image.png ,then i encoded in json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.