android - images url are not displaying in grid -
i have set gridview image url..i can't able see image in grid.. shows background...and on click grid plays in next screen.
what im doing wrong? how implement this?
thanks lot in advance
my code
public class act extends activity { static string uri1="https://i3.ytimg.com/vi/bqawsvqsldy/default.jpg"; static string uri2="https://i4.ytimg.com/vi/cjqcniwqdno/mqdefault.jpg"; static string uri3="https://i1.ytimg.com/vi/d8da4pe5hey/mqdefault.jpg"; public static string[] urls={uri1,uri2,uri3}; public bitmap bitmap; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); gridview grd=(gridview)findviewbyid(r.id.gridview1); grd.setadapter(new imageadapter(this)); grd.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent,view v,int pos,long id) { toast.maketext(getbasecontext(),"pic"+(pos+1)+"select ",toast.length_short).show(); } }); } public class imageadapter extends baseadapter { private context context; private int itembackground; imageadapter(context c) { context=c; typedarray a=obtainstyledattributes(r.styleable.gallery1); itembackground=a.getresourceid(r.styleable.gallery1_android_galleryitembackground,0); a.recycle(); } public int getcount() { return urls.length; } public object getitem(int pos) { return pos; } public long getitemid(int pos) { return pos; } public view getview(int pos,view cv,viewgroup vg) { bitmap bitmap= downloadimage( urls[pos] ); imageview imageview=new imageview(context); imageview.setimagebitmap(bitmap); return cv; } private bitmap downloadimage(string url) { final string url1=url; new thread() { public void run() { inputstream in = null; message msg = message.obtain(); msg.what = 1; try { in = openhttpconnection(url1); bitmap bitmap = bitmapfactory.decodestream(in); bundle b = new bundle(); b.putparcelable("bitmap", bitmap); msg.setdata(b); in.close(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } }.start(); return bitmap; } private inputstream openhttpconnection(string urlstring) throws ioexception { inputstream in = null; int response = -1; url url = new url(urlstring); urlconnection conn = url.openconnection(); if (!(conn instanceof httpurlconnection)) throw new ioexception("not http connection"); try{ httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setallowuserinteraction(false); httpconn.setinstancefollowredirects(true); httpconn.setrequestmethod("get"); httpconn.connect(); response = httpconn.getresponsecode(); if (response == httpurlconnection.http_ok) { in = httpconn.getinputstream(); } } catch (exception ex) { throw new ioexception("error connecting"); } return in; } } }
logcat
e/androidruntime(832): java.lang.nullpointerexception e/androidruntime(832): @ android.widget.gridview.onmeasure(gridview.java:937) e/androidruntime(832): @ android.view.view.measure(view.java:8313) e/androidruntime(832): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:3138) e/androidruntime(832): @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1017) e/androidruntime(832): @ android.widget.linearlayout.measurevertical(linearlayout.java:386) e/androidruntime(832): @ android.widget.linearlayout.onmeasure(linearlayout.java:309) e/androidruntime(832): @ android.view.view.measure(view.java:8313) e/androidruntime(832): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:3138) e/androidruntime(832): @ android.widget.framelayout.onmeasure(framelayout.java:250) e/androidruntime(832): @ android.view.view.measure(view.java:8313) e/androidruntime(832): @ android.widget.linearlayout.measurevertical(linearlayout.java:531) e/androidruntime(832): @ android.widget.linearlayout.onmeasure(linearlayout.java:309) e/androidruntime(832): @ android.view.view.measure(view.java:8313) e/androidruntime(832): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:3138) e/androidruntime(832): @ android.widget.framelayout.onmeasure(framelayout.java:250) e/androidruntime(832): @ android.view.view.measure(view.java:8313) e/androidruntime(832): @ android.view.viewroot.performtraversals(viewroot.java:839) e/androidruntime(832): @ android.view.viewroot.handlemessage(viewroot.java:1859) e/androidruntime(832): @ android.os.handler.dispatchmessage(handler.java:99) e/androidruntime(832): @ android.os.looper.loop(looper.java:123) e/androidruntime(832): @ android.app.activitythread.main(activitythread.java:3683) e/androidruntime(832): @ java.lang.reflect.method.invokenative(native method) e/androidruntime(832): @ java.lang.reflect.method.invoke(method.java:507) e/androidruntime(832): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) e/androidruntime(832): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) e/androidruntime(832): @ dalvik.system.nativestart.main(native method)
this code tried ..images displaying..you customize grid view need.you copy , paste below code in activity , try..
public class mainactivity extends activity { static string uri1="http://i3.ytimg.com/vi/bqawsvqsldy/default.jpg"; static string uri2="http://i4.ytimg.com/vi/cjqcniwqdno/mqdefault.jpg"; static string uri3="http://i1.ytimg.com/vi/d8da4pe5hey/mqdefault.jpg"; public static string[] urls={uri1,uri2,uri3}; // public bitmap bitmap; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview grd=(gridview)findviewbyid(r.id.gridview1); grd.setadapter(new imageadapter(this)); } public class imageadapter extends baseadapter { private context context; private int itembackground; imageadapter(context c) { context=c; } public int getcount() { return urls.length; } public object getitem(int pos) { return pos; } public long getitemid(int pos) { return pos; } private bitmap downloadimage(string url) { string url1=url; bitmap bitmap = null; // new thread() // { // public void run() // { inputstream in = null; message msg = message.obtain(); msg.what = 1; try { in = openhttpconnection(url1); bitmap = bitmapfactory.decodestream(in); bundle b = new bundle(); b.putparcelable("bitmap", bitmap); msg.setdata(b); in.close(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } // } // }.start(); return bitmap; } private inputstream openhttpconnection(string urlstring) throws ioexception { // system.out.println("insdie conn"); inputstream in = null; int response = -1; url url = new url(urlstring); urlconnection conn = url.openconnection(); if (!(conn instanceof httpurlconnection)) throw new ioexception("not http connection"); try{ // system.out.println("inside try"); httpurlconnection httpconn = (httpurlconnection) conn; httpconn.setallowuserinteraction(false); httpconn.setinstancefollowredirects(true); // httpconn.setrequestmethod("get"); httpconn.connect(); response = httpconn.getresponsecode(); // system.out.println("res="+response); // system.out.println("cccccc="+httpurlconnection.http_ok); if (response == httpurlconnection.http_ok) { // system.out.println("inside if"); in = httpconn.getinputstream(); } } catch (exception ex) { throw new ioexception("error connecting"); } return in; } @override public view getview(int position, view cv, viewgroup parent) { imageview imageview = null; // system.out.println("vvvv="+urls[position]); bitmap bitmap= downloadimage( urls[position] ); // todo auto-generated method stub if(cv == null) { // cv=layoutinflater.from(parent.getcontext()).inflate(r.layout.gridviewitem, null); imageview = new imageview(context); } else { imageview = (imageview) cv; } imageview.setimagebitmap(bitmap); return imageview; } }