android - How to convert the URL path into image and showing on the list view -
based on location trying search stores , search result showing on list view of cell . getting name,address,related icons , rating google problem how show image on list view of list cell.i got url path of image.i convert url path bitmapimages using bitmap getbitmapfromurl().but showing npe(null pointer exception). below going paste code.
public class nearbyactivity extends activity implements locationlistener { // private listadapter adapter; listview lvplaces; imageview ivphone,ivcamera,ivprinter,ivmore; string name,vicinity,formatted_phone_number,rating,icon; double mlatitude=0; double mlongitude=0; sharedpreferences spref; arraylist<hashmap<string, string>> placeslistitems = new arraylist<hashmap<string,string>>(); // customlistadapter customlistadapter; public imageloader imageloader; private static final string name = "name"; private static final string vicinity = "vicinity"; private static final string rating="rating"; private static final string icon="icon"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.nearby); lvplaces=(listview)findviewbyid(r.id.lvplaces); final arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_list_item_1); lvplaces.setadapter(adapter); lvplaces.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> mainactivity, final view view,int position, long id) { final textview tvvicinity=(textview)view.findviewbyid(r.id.tvvicinity); final textview tvrating=(textview)view.findviewbyid(r.id.tvrating); final textview tvname=(textview)view.findviewbyid(r.id.tvname); final imageview ivphone = (imageview)findviewbyid(r.id.ivphone); final imageview ivcamera = (imageview)findviewbyid(r.id.ivcamera); final imageview ivprinter = (imageview)findviewbyid(r.id.ivprinter); final imageview ivmore = (imageview)findviewbyid(r.id.ivmore); ivphone.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getapplication(),"do want call",toast.length_short).show(); } }); ivcamera.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getapplication(),"do want take pics",toast.length_short).show(); } }); ivprinter.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getapplication(),"do want print",toast.length_short).show(); } }); ivmore.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getapplication(),"do want more",toast.length_short).show(); } }); tvname.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string getname=tvname.gettext().tostring(); string getvicinity=tvvicinity.gettext().tostring(); string getrating=tvrating.gettext().tostring(); intent = new intent(getapplication(),information.class); i.putextra(name,getname); i.putextra(vicinity,getvicinity); i.putextra(rating,getrating); startactivity(i); } }); } }); locationmanager locationmanager = (locationmanager) getsystemservice(location_service); // creating criteria object retrieve provider criteria criteria = new criteria(); // getting name of best provider string provider = locationmanager.getbestprovider(criteria, true); // getting current location gps location location = locationmanager.getlastknownlocation(provider); if(location!=null) { onlocationchanged(location); } locationmanager.requestlocationupdates(provider,20000,0,this); stringbuilder sb = new stringbuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); sb.append("location="+mlatitude+","+mlongitude); sb.append("&radius=1000"); sb.append("&types=beauty_salon"); sb.append("&sensor=true"); sb.append("&key=aizasybgtvo5lfndqfy8w44tfmevgg2klgj5wea"); // creating new non-ui thread task download json data placestask placestask = new placestask(); // invokes "doinbackground()" method of class placetask placestask.execute(sb.tostring()); } /** method download json data url */ private string downloadurl(string strurl) throws ioexception { string data = ""; inputstream istream = null; httpurlconnection urlconnection = null; try{ url url = new url(strurl); // creating http connection communicate url urlconnection = (httpurlconnection) url.openconnection(); // connecting url urlconnection.connect(); // reading data url istream = urlconnection.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(istream)); stringbuffer sb1 = new stringbuffer(); string line = ""; while( ( line = br.readline()) != null) { sb1.append(line); } data = sb1.tostring(); br.close(); } catch(exception e) { log.d("exception while downloading url", e.tostring()); } { istream.close(); urlconnection.disconnect(); } return data; } @override public void onlocationchanged(location location) { mlatitude = location.getlatitude(); mlongitude = location.getlongitude(); } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } public bitmap getbitmapfromurl(string icon ) { try { url url = new url(icon); 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; } } private class placestask extends asynctask<string, integer, string> { string data = null; @override protected string doinbackground(string... url) { try { data = downloadurl(url[0]); } catch(exception e) { log.d("background task",e.tostring()); } return data; } @override protected void onpostexecute(string result) { parsertask parsertask = new parsertask(); // start parsing google places in json format // invokes "doinbackground()" method of class parsetask parsertask.execute(result); } } private class parsertask extends asynctask<string, integer, list<hashmap<string,string>>> { @override protected list<hashmap<string, string>> doinbackground(string... jsondata) { jsonobject jobject; list<hashmap<string, string>> places = null; placejsonparser placejsonparser = new placejsonparser(); try { jobject = new jsonobject(jsondata[0]); /** getting parsed data list construct */ places = placejsonparser.parse(jobject); } catch(exception e) { log.d("exception",e.tostring()); } return places; } @override protected void onpostexecute(list<hashmap<string,string>> list) { for(int i=0;i<list.size();i++) { hashmap<string, string> hmplace = list.get(i); name = hmplace.get("place_name"); vicinity = hmplace.get("vicinity"); rating=hmplace.get("rating"); formatted_phone_number = hmplace.get("phone_number"); icon = hmplace.get("icon"); placeslistitems.add(hmplace); } toast.maketext(getapplication(),""+icon,toast.length_short).show(); imageview ivpics = (imageview)findviewbyid(r.id.ivpics); ivpics.setimagebitmap(getbitmapfromurl(icon)); listadapter adapter=new simpleadapter(nearbyactivity.this,placeslistitems, r.layout.myplace, new string[]{"place_name","vicinity","rating","phone_number"}, new int[]{r.id.tvname,r.id.tvvicinity,r.id.tvrating,r.id.ivphone}); lvplaces.setadapter(adapter); } }
logcat:
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.imageview.setimagebitmap(android.graphics.bitmap)' on null object reference @ info.androidhive.slidingmenu.activities.nearbyactivity$parsertask.onpostexecute(nearbyactivity.java:328) @ info.androidhive.slidingmenu.activities.nearbyactivity$parsertask.onpostexecute(nearbyactivity.java:289) @ android.os.asynctask.finish(asynctask.java:632) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:645) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5312) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:901) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:696)
try
url url = new url("http://abcd.com/resize?sq=60&uid=2216744464"); bitmap bmp = bitmapfactory.decodestream(url.openconnection().getinputstream()); imageview.setimagebitmap(bmp);
Comments
Post a Comment