• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps PNG file won't display in ImageView when src is set programatically

I have a png file in my res/drawable-ldpi folder. I'm working in Eclipse with the emulator, but I've tried this on my 2.2.1 Android phone and I get the same behavior. I have an ImageView and I want to set the src programatically, based on a database call. If I just put
Code:
src="@drawable.norusdpyr"
in the Activity's XML file, the file displays fine. BUT, if I put
Code:
String imageresource = "R.drawable." + parsedData[4].toString();
    chart.setImageURI(Uri.parse(imageresource));
where parsedData[4].toString() = "nor_usdpyr" I don't see anything. I've also tried
Code:
 String imageresource = "android.resource://" + getPackageName() + "/R.drawable." + parsedData[4].toString();
    chart.setImageURI(Uri.parse(imageresource));
and
Code:
InputStream is = getClass().getResourceAsStream("/drawable/" + parsedData[4].toString());
chart.setImageDrawable(Drawable.createFromStream(is, ""));
and
Code:
String imageresource = "R.drawable." + parsedData[4].toString();   
    File file = new File(imageresource);
    chart.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));
and
Code:
Context context = getApplicationContext();
    int ResID = context.getResources().getIdentifier(imageresource, "drawable", "com.KnitCard.project");
    chart.setImageResource(ResID);
finally,
Code:
 chart.setImageURI(Uri.parse("android.resource://" + getPackageName() + "/R.drawable.nor_usdpyr"));
doesn't work.
None of these work. What am I doing wrong? Thanks!
 
In the layout XML, you should use a / instead of a .
Code:
src="@drawable/norusdpyr"

Or in an activity, set the image like this:
Code:
chart.setImageDrawable(R.drawable.norusdpyr)
 
The . instead of a / was a typo on my part. However, the setImageDrawable method takes a parameter of type Drawable, so my question would be how to I convert a string that I build dynamically into a Drawable? At any rate, I've found a solution using setImageResource:

Code:
 [/LEFT]
[SIZE=2]String imageresource = parsedData[4].toString();
[/SIZE][LEFT]Context context = getApplicationContext();
int ResID = context.getResources()
.getIdentifier(imageresource, "drawable",
context.getPackageName());[/LEFT]
chart.setImageResource(ResID);
[LEFT]

Thanks for your help! :)
 
Back
Top Bottom