android - How To Create a Simple Universal Image Loader via URL(Only one image) -
i done don't know why image does't desplay. , told me after how can put images grid views.)
public class mainactivity extends actionbaractivity { private imageview imageview; private bitmap bitmap; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imageview = (imageview) findviewbyid(r.id.imageview1); bitmap=getbitmapfromurl("http://cs596.vk.me/u78792285/a_98dc2d1e.jpg"); imageview.setimagebitmap(bitmap); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public 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 bitmap= bitmapfactory.decodestream(input); return bitmap; } catch (exception e) { // todo: handle exception e.printstacktrace(); return null; } } }
try code, both httpurlconnection , updating image image view need in thread or runnable.
note how changed oncreate
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setbitmapfromurl("http://cs596.vk.me/u78792285/a_98dc2d1e.jpg"); } public void setbitmapfromurl(string src) { try { new thread(new runnable() { public void run() { url url=new url(src); httpurlconnection connection= (httpurlconnection) url.openconnection(); connection.setdoinput(true); connection.connect(); inputstream input=connection.getinputstream(); bitmap bitmap= bitmapfactory.decodestream(input); if(bitmap!=null) { try { final bitmap mutablebitmap = bitmap.copy(bitmap.config.argb_8888, true); runonuithread(new runnable() { @override public void run() { imageview = (imageview) findviewbyid(r.id.imageview1); imageview.setimagebitmap(bitmap); } }); } catch(outofmemoryerror e) { } } } } } catch (exception e) { // todo: handle exception e.printstacktrace(); } }
Comments
Post a Comment