android - Setting an imageview of UI from another class with Asynctask? -
how can set image in ui class running asynctask image (bitmap) url? think can use asynctask.get() in ui when thread finishes think not best way, here code of activity , class dowload image:
public class recipe extends activity { private imageview image; private static final string baseurl = "http://x/images/"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.recipe_activity); intent = getintent(); getrecipe getr = new getrecipe(i.getstringextra("id")); getr.getid(); textview title = (textview) findviewbyid(r.id.tvrecipetitle); title.settext(getr.getname()); textview description = (textview) findviewbyid(r.id.tvrecipedescription); description.settext(getr.getdescription()); string imageurl = getr.getimage(); log.d("recipe", "setting image"); image = (imageview) findviewbyid(r.id.ivrecipe); log.d("recipe", "image url: " + imageurl); // download image getrecipeimage gri = new getrecipeimage(baseurl + imageurl, this); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.recipe, menu); return true; } // method should called when image full downloaded public void setimageview(bitmap bp) { image.setimagebitmap(bp); } } public class getrecipeimage { private context context; private string url; // private bitmap downloadedimage = null; // private asynctask<void, void, void> giasynctask; public getrecipeimage(string url, context context) { this.context = context; this.url = url; new downloadimagetask().execute(); } public static bitmap getbitmapfromasset(context con) { assetmanager assetmanager = con.getassets(); inputstream istr; bitmap bitmap = null; try { istr = assetmanager.open("imagenotfound.png"); bitmap = bitmapfactory.decodestream(istr); } catch (ioexception e) { return null; } return bitmap; } private bitmap imagenotfound() { return getbitmapfromasset(context); } private class downloadimagetask extends asynctask<string, void, bitmap> { protected bitmap doinbackground(string... urls) { log.d("gri", "getting image"); string urldisplay = urls[0]; bitmap micon11 = null; try { inputstream in = new java.net.url(urldisplay).openstream(); micon11 = bitmapfactory.decodestream(in); } catch (exception e) { log.e("error", e.getmessage()); e.printstacktrace(); } if (micon11 == null) { return imagenotfound(); } else { return micon11; } } protected void onpostexecute(bitmap result) { // context.getclass().getsimplename().setimageview(); } } }
thank attention!
why getting context if asynctask inner class??
just call method of class call recipe.this
..
protected void onpostexecute(bitmap result) { recipe.this.setimageview(result); }
Comments
Post a Comment