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