1

I'm trying to display an image into imageview in android from php mysql..I'm using JSON to parse the image..

 nom | couleur | image  
 -------------------------------------------------------------------  
 passat | gris |http://10.0.2.2/model/image/passat.jpg  

My php code:

$result = mysql_query("SELECT * FROM info WHERE nom = '$nom'");  

if (!empty($result)) {  
    // check for empty result  
    if (mysql_num_rows($result) > 0) {  

        $result = mysql_fetch_array($result);  

        $info = array();  

        $info["nom"] = $result["nom"];  
        $info["couleur"] = $result["couleur"];  
        $info["prix"] = $result["prix"];  
        $info["image"] = base64_encode($result["image"]);  
        // success  
        $response["success"] = 1;  

        // user node  
        $response["info"] = array();  

        array_push($response["info"], $info);  

        // echoing JSON response  
        echo json_encode($response); 

    }
}

and here is my Info.java code:

package com.example.gridview2;  

import java.util.ArrayList;  
import java.util.List;  

import org.apache.http.NameValuePair;  
import org.apache.http.message.BasicNameValuePair;  
import org.json.JSONArray;  
import org.json.JSONException;  
import org.json.JSONObject;  

import android.app.Activity;  
import android.content.Intent;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.os.Bundle;  
import android.util.Base64;  
import android.util.Log;  
import android.widget.ImageView;  
import android.widget.TextView;  

public class Info extends Activity {

    TextView txt1,txt2,txt3;  
    ImageView image;  
    JSONParser jParser = new JSONParser();  
    private static final String url_product_detials = "http://10.0.2.2/model/detail.php";  
    private static final String TAG_SUCCESS = "success";  
    private static final String TAG_PRODUCT = "info";  
    private static final String TAG_NAME = "nom";  
    private static final String TAG_COLOR = "couleur";  
    private static final String TAG_PRICE = "prix";  
    private static final String TAG_IMAGE = "image";  

    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main7);  

        process();  
    }
    private void process() {  
        Intent intent = getIntent();  
        String nom = intent.getStringExtra(TAG_NAME);  
        List<NameValuePair> params = new ArrayList<NameValuePair>();  
        params.add(new BasicNameValuePair(TAG_NAME, nom));  
        JSONObject json = jParser.makeHttpRequest(url_product_detials, "GET",
                params);  
        Log.d("Single employee Details", json.toString());  
        try {  

            int success = json.getInt(TAG_SUCCESS);  

            if (success == 1) {  
                JSONArray productObj = json.getJSONArray(TAG_PRODUCT); // JSON Array  

                // get first product object from JSON Array  
                JSONObject product = productObj.getJSONObject(0);  

                txt1 = (TextView) findViewById(R.id.textView1);  
                txt2 = (TextView) findViewById(R.id.textView2);  
                txt3 = (TextView) findViewById(R.id.textView3);  
                image = (ImageView) findViewById(R.id.imageView1);  

                String NOM =product.getString(TAG_NAME);  
                String couleur = product.getString(TAG_COLOR);  
                String prix =   product.getString(TAG_PRICE);  
                String image1 =   product.getString(TAG_IMAGE);  
                txt1.setText(NOM);  
                txt2.setText(couleur);  
                txt3.setText(prix);  
                byte[] encodeByte = Base64.decode(image1, Base64.DEFAULT);  
                Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);       

                image.setImageBitmap(bitmap);  

            } else{  
                // product with pid not found
            }  
        } catch (JSONException e) {  
            e.printStackTrace();  
        }  
    }  
}

my json response:

{
  "success": 1,
  "info": [
    {
      "nom": "passat",
      "couleur": "gris",
      "prix": "1000",
      "image": "aHR0cDovLzEwLjAuMi4yL21vZGVsL2ltYWdlL3Bhc3NhdC5qcGc="
    }
  ]
}

image doesn't show , i don't know where is the problem
can any one help me

ugo
  • 2,705
  • 2
  • 30
  • 34
user1893
  • 83
  • 1
  • 2
  • 8
  • Are you getting any errors? If so, post the logcat. Second, I noticed that you are not calling process in a separate thread, this will cause an error as you can not make any network calls on the main thread. – KennyC May 05 '14 at 18:30
  • NO i'm not getting any errors,how can i fix the problem – user1893 May 05 '14 at 18:33
  • You are trying to create the bitmap from a byte array, but the byte array is just a link to the image, not the actual image data. You need to take that link and then download the image from it. – KennyC May 05 '14 at 18:37
  • I'm SORRY but i don' know how to do it ,i'm asking for your help thunk you very muth – user1893 May 05 '14 at 18:41
  • Search for it, there are dozens of answers, here's one I found really quick http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – KennyC May 05 '14 at 18:44
  • byte[] encodeByte = Base64.decode(image1, Base64.NO_WRAP); InputStream inputStream = new ByteArrayInputStream(encodeByte); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); image.setImageBitmap(bitmap); i tried this but it doesn't work – user1893 May 05 '14 at 19:41
  • You have to take your decoded bytes and turn them into a String. That string is going to be the URL of the image (http://10.0.2.2/model/image/passat.jpg). The InputStream should then be InputStream in = new java.net.URL(DECODED_URL).openStream(); – KennyC May 05 '14 at 19:46
  • byte[] encodeByte = Base64.decode(image1, Base64.DEFAULT); String str = new String(encodeByte); InputStream inputStream = new java.net.URL(str).openStream(); BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream,null,options);it's worrrrk thaaank you very much for your help – user1893 May 05 '14 at 20:16

0 Answers0