1

I've been trying several ways of displaying my JSON images but my solutions never succeeded.

I've parsed my JSON and I'm getting the response.

However included the response is an image which I need to display in my application.

Recieving the image as :"img src=question/images/u2_1_l2_q66" which is just string. I'm getting the same text as in emulator "not a image".

In drawable, should I add the image ?

How can I display these image in my application ? Please guide me.

Any help would be appreciable.

code

       protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tid", tid));
        json = jsonParser.makeHttpRequest(url_get_quesurl, "GET", params);
        Log.d("All Groups: ", json.toString());
        try {
        int success = json.getInt(TAG_SUCCESS);
        if (success == 1) {
            System.out.println("Success");
            groups = json.getJSONArray(TAG_GROUP);
            System.out.println("Result Success+++"+groups);
             for (int i = 0; i < groups.length();i++) {
            JSONObject c = groups.getJSONObject(i);
            String question = c.getString(TAG_QUES);
            System.out.println("Checking ::"+question);
            ques1.add(question);
            String img = c.getString(TAG_IMAGE);
            System.out.println("Checking ::"+img);
            imgarr.add(img);
             }
        } else {
            showAlert();
        }
    } catch (JSONException e) {
        System.out.println("Error "+e.toString());
    }
    return null;
}
protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        ques1=new ArrayList<String>(new ArrayList<String>(ques1));
        imgarr=new ArrayList<String>(new ArrayList<String>(imgarr));
        TextView txtque = (TextView) findViewById(R.id.que_txt); 
        txtque.setText("Q" + num + ")" + ques1.get(j) + imgarr.get(g));
           }

php code

      $group[$i]['image']= base64_encode(file_get_contents("http://10.0.2.2/question/images/".$row['image']));                                                                                                     
        $i++;
      array_push($response["group"], $group);
8
  • What you getting in the tag TAG_IMAGE? Commented Feb 19, 2013 at 7:31
  • Change your image in php to base64 and then decode it in image in android.I can give you the code Commented Feb 19, 2013 at 7:33
  • @kumarandroid in TAG_IMAGE only im getting like this "img src=question/images/u2_1_l2_q66" Commented Feb 19, 2013 at 7:57
  • @sammarjaved i can't understand can explain it.... Commented Feb 19, 2013 at 7:59
  • Please guide me to fix my problem.. Commented Feb 19, 2013 at 8:07

2 Answers 2

1
<?php

mysql_connect("localhost","root","sarwar");
mysql_select_db("ess");
$sql=mysql_query("select image from default_products ");

$i=0;

while($row=mysql_fetch_assoc($sql))
{   




//Where Image Exists.
//First taking path from database and then image from folder against that path and then //converting it ino base64 and then Json
   $output[$i]['image']= base64_encode(file_get_contents("http://ur ip address/ess/uploads/default/products/".$row['image']));



  $i++;
}


print(json_encode($output));




mysql_close();
?>
Sign up to request clarification or add additional context in comments.

1 Comment

this is php code first apply it according to your database and folder names
1
String image;
protected Void doInBackground(String... arg0) {



                try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("//Ur URL");

                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                         is = entity.getContent();
                }catch(Exception e){
                        Log.e("log_tag", "Error in http connection "+e.toString());
                }

                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();

                        result=sb.toString();
                }catch(Exception e){
                        Log.e("log_tag", "Error converting result "+e.toString());
                }
                return null;
                // TODO Auto-generated method stub

            }
            protected void onPostExecute(Void v) {
                  try{


                        JSONArray jArray = new JSONArray(result);
                      for(int i=0;i<=jArray.length();i++)
                      {
                                JSONObject json_data = jArray.getJSONObject(i);


                            String  image=json_data.getString("image");








                      }

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



                                                  imageview.setImageBitmap(bmp);


                }

                catch(JSONException e){
                        Log.e("log_tag", "Error parsing data "+e.toString());
                }

            }

1 Comment

this will show only one image if you want to show all images coming from MySQL then Create Image View at run time inside the for loop.and put code above the catch block inside for loop

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.