android - How to put an image in an ImageView from the Activity not the XML file? -
what trying put multiple images without out of memory problem i'm using method putimage() , trying put image in imageview activity not xml file
here xml code
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" android:alwaysdrawnwithcache="false"> <imageview android:id="@+id/imageview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignparenttop="true" /> </relativelayout>
and here activity
public class recipebanana extends activity { imageview v; button button; context localcontext = null; public static drawable getassetimage(context context, string filename) throws ioexception { assetmanager assets = context.getresources().getassets(); inputstream buffer = new bufferedinputstream((assets.open("drawable/" + filename + ".jpg"))); bitmap bitmap = bitmapfactory.decodestream(buffer); return new bitmapdrawable(bitmap); } public void putimage(imageview v, string x){ try { v.setimagedrawable(drawable.createfromstream(localcontext.getassets().open("flags/" + x + ".jpg"), null)); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_recipe_banana); imageview v=(imageview) findviewbyid(r.id.imageview1); putimage(v,"bananarecipe"); addlisteneronbutton(); }
you aren't ever calling getassetimage() don't need method, remove it. don't need create variable hold reference context. activity is a context allowed call getassets()
directly without needing put in front of it.
try this:
public class recipebanana extends activity { imageview v; button button; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_recipe_banana); imageview v=(imageview) findviewbyid(r.id.imageview1); putimage(v,"bananarecipe"); addlisteneronbutton(); } public void putimage(imageview v, string filename){ try { v.setimagedrawable(drawable.createfromstream(getassets().open("flags/" + filename + ".jpg"), null)); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
using code if have file called bananarecipe.jpg
inside of assets/flags/
should load imageview.
Comments
Post a Comment