android - PNG file won't display in ImageView if source is set programatically -
i have png file in res/drawable-ldpi folder. i'm working in eclipse emulator, i've tried on 2.2.1 android phone , same behavior. have imageview , want set src programatically, based on database call. if put
src="@drawable.norusdpyr"
in activity's xml file, file displays fine. but, if put
string imageresource = "r.drawable." + parseddata[4].tostring(); chart.setimageuri(uri.parse(imageresource));
where parseddata[4].tostring() = "nor_usdpyr" don't see anything. i've tried
string imageresource = "android.resource://" + getpackagename() + "/r.drawable." + parseddata[4].tostring(); chart.setimageuri(uri.parse(imageresource));
and
inputstream = getclass().getresourceasstream("/drawable/" + parseddata[4].tostring()); chart.setimagedrawable(drawable.createfromstream(is, ""));
and
string imageresource = "r.drawable." + parseddata[4].tostring(); file file = new file(imageresource); chart.setimagedrawable(drawable.createfrompath(file.getabsolutepath()));
and
context context = getapplicationcontext(); int resid = context.getresources().getidentifier(imageresource, "drawable", "com.knitcard.project"); chart.setimageresource(resid);
finally,
chart.setimageuri(uri.parse("android.resource://" + getpackagename() + "/r.drawable.nor_usdpyr"));
doesn't work.
none of these work. doing wrong? thanks!
first, shouldn't working:
src="@drawable.norusdpyr"
the correct way is:
android:src="@drawable/norusdpyr"
second, string ids use in xml valid in xml. when resources built, strings replaced respective integer values. ideally, won't need load package resource string name. if do, can field such name, integral value holds, , call setimageresource
. quite close version:
int resid = context.getresources().getidentifier(imageresource, "drawable", "com.knitcard.project"); chart.setimageresource(resid);
except imageresource
should have been parseddata[4].tostring()
no r.drawable.
prefix. can use context.getpackagename()
instead of hardcoding package name.
Comments
Post a Comment