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'];

    }
ammu
  • 117
  • 2
  • 14

3 Answers3

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);
NullPointerException
  • 3,978
  • 4
  • 34
  • 52
  • 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 :) – San Feb 01 '14 at 07:26
0
JSONObject obj = new JSONObject(respponse);

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

JSONObject data = arr.GetJSONObject(0);

String image_url = data.GetString("image_url");
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34
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

Community
  • 1
  • 1
San
  • 5,567
  • 2
  • 26
  • 28
  • i didnt use any url, image_url is my table column name,i stored my image.png ,then i encoded in json. – ammu Feb 01 '14 at 07:49