I am trying to parse image from server,and I am using a query, in my Logcat response is showing but image is not displaying! The following is my response of Json and snippet code.
Can anyone help?
[
{
"b_card":"http:\/\/www.webname.com\/uploaded\/users\/vcard\/8_ers.jpg"
}
]
And BusinessCard.java:
public class BusinessCard extends Activity{
private Button btn;
private String User_id;
private Activity activity;
private ImageView bucard;
ImageLoader _DownImageLoader;
AQuery androidAQuery=new AQuery(this);
JSONArray state_list=null;
private static String ALL_SUBCATAGORY_URL = "";
private static final String ALL_SUBCATAGORY_ID="b_card";
ArrayList<HashMap<String,String>> subcatagorydata;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.business_card);
User_id=this.getIntent().getStringExtra("userids");
System.out.println("for business card"+User_id);
ALL_SUBCATAGORY_URL="http://www.asdf.com/web-service/b_card.php?user_id="+User_id;
/*ImageLoader imageLoader = new ImageLoader(activity.getApplicationContext());
ImageView Profileimage = (ImageView) findViewById(R.id.You_image_view);
imageLoader.DisplayImage(imageurl , Profileimage);*/
bucard=(ImageView)findViewById(R.id.imgvwbusinesscard);
btn=(Button)findViewById(R.id.dwnldbutton);
_DownImageLoader = new ImageLoader(activity);
bucard.setTag(ALL_SUBCATAGORY_URL); //set url tag to image
_DownImageLoader.DisplayImage(ALL_SUBCATAGORY_URL, activity ,bucard);
new LoadAllSubcatagories().execute();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
class LoadAllSubcatagories extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
ArrayAdapter<String> adapterallsubcatagory ;
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
/* pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Load All Subcatagories..");
pDialog.setIndeterminate(false);
// pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(true);
pDialog.show();*/
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
subcatagorydata = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(ALL_SUBCATAGORY_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
//JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
//JSONArray data_array = new JSONArray(received);
state_list = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < state_list.length(); i++) {
JSONObject c = state_list.getJSONObject(i);
String card=state_list.getJSONObject(i).toString();
System.out.println("Card "+card);
androidAQuery.id(bucard).image(card, true, true);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(ALL_SUBCATAGORY_ID, c.getString(ALL_SUBCATAGORY_ID));
subcatagorydata.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return subcatagorydata;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
}
}
}
first of all ... get the json response into a string
.. and your response is looks like a link
.. so its a link of image but not a image .. so then you need image loader
to load image from link to imageview
.. you can universal-image-loader
.. from this link you can download the UIL
library ... then you can use it to load the image
add UIL
then use this to load image
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
ImageView Profileimage = (ImageView) findViewById(R.id.You_image_view);
imageLoader.DisplayImage(imageurl , Profileimage);
if not know how to use UIL then add this java class(ImageLoader.java)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.widget.ImageView;
public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
final int stub_id=R.drawable.defaultpic;
public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
Bitmap output;
if(bitmap!=null)
{
output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 9;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
imageView.setImageBitmap(output);
}
else
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p=new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=1024;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag=imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
Bitmap out;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null)
{
out = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(out);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 9;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
photoToLoad.imageView.setImageBitmap(out);
}
else
{
photoToLoad.imageView.setImageResource(stub_id);
}
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}