Load Images probelm from url in Android -
i trying load images url , set in gridview, when scroll screen images changes. beginner in android. don't know problem in code. here code..
public class photos extends activity { public static final string tag_image_name = "image_name"; public static final string tag_image_thumb_name = "image_thumb_name"; public static string url = "http://...../..../..../mainapi.php"; arraylist<hashmap<string, string>> photolist; string responsedata = null; static gridview gridview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.photos); gridview = (gridview)findviewbyid(r.id.gridview); photolist = new arraylist<hashmap<string,string>>(); new asyncdata().execute(); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { // sending image id fullscreenactivity intent = new intent(getapplicationcontext(), fullimage.class); // passing array index i.putextra("imagename", tag_image_name); startactivity(i); } }); } class asyncdata extends asynctask<string, void, void> { progressdialog pdialog; @override protected void onpreexecute() { pdialog = new progressdialog(photos.this); pdialog.settitle("loading...."); pdialog.setmessage("please wait..."); pdialog.show(); super.onpreexecute(); } @override protected void doinbackground(string... args) { // todo auto-generated method stub httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("rquest","{\"method\":\"photogallery\",\"body\":[{}]}")); httppost.setentity(new urlencodedformentity(namevaluepairs, http.utf_8)); httpresponse response = httpclient.execute(httppost); httpentity resentity = response.getentity(); responsedata = entityutils.tostring(resentity); try { jsonarray data = new jsonarray(responsedata); (int = 0; < data.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); jsonobject c = data.getjsonobject(i); string photoname = c.getstring(tag_image_name); string imagethumbname = c.getstring(tag_image_thumb_name); map.put(tag_image_name, photoname); map.put(tag_image_thumb_name, imagethumbname); photolist.add(map); } } catch (jsonexception e) { // todo: handle exception e.printstacktrace(); } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(void result) { super.onpostexecute(result); gridview.setadapter(new photosadapter(photos.this, r.layout.photo_row, photolist)); if (pdialog != null && pdialog.isshowing()) { pdialog.dismiss(); } } } @override public boolean onkeydown(int keycode, keyevent event) { if(keycode == keyevent.keycode_back && event.getrepeatcount() == 0) { finish(); return true; } return super.onkeydown(keycode, event); } }
phgotosadapter class
public class photosadapter extends arrayadapter<hashmap<string, string>>{ context context; string uri; bitmap bitmap; arraylist<hashmap<string, string>> mylist; hashmap<string, string> mydata; int layout; public photosadapter(context context, int textviewresourceid, list<hashmap<string, string>> objects) { super(context, textviewresourceid, objects); // todo auto-generated constructor stub this.context = context; this.mylist = (arraylist<hashmap<string, string>>) objects; this.layout = textviewresourceid; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub view row = null; layoutinflater inflater = ((activity) context).getlayoutinflater(); row = inflater.inflate(layout, parent, false); mydata = mylist.get(position); imageview image = (imageview)row.findviewbyid(r.id.imagephoto); try{ imagedownloadtask task = new imagedownloadtask(image); task.execute(); uri = "" + mydata.get(photos.tag_image_thumb_name).replace(" ", "%20"); image.setimagebitmap(bitmap); image.setscaletype(imageview.scaletype.center_inside); } catch (exception e) { // todo: handle exception e.printstacktrace(); } return row; } public static bitmap getbitmapfromurl(string src) { try { url url = new url(src); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); bitmap mybitmap = bitmapfactory.decodestream(input); return mybitmap; } catch (ioexception e) { e.printstacktrace(); return null; } } class imagedownloadtask extends asynctask<void, integer, bitmap> { private imageview mview; progressdialog pdialog; imagedownloadtask(imageview view){ mview = view; } @override protected bitmap doinbackground(void... params) { try { bitmap = getbitmapfromurl(uri); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return bitmap; } @override protected void onpostexecute(bitmap result) { mview.setimagebitmap(result); } } }
i don't know problem in it, please me , give solution.
imageloader imageloader; imageloader=new imageloader(context.getapplicationcontext()); imageloader.displayimage(urlname, imageviewname); try
Comments
Post a Comment