I have a list of entries and some bitmap files in the res/drawable-mdpi directory. I\'m trying to load the image corresponding to the string value selected from the list by
You can Create Common Function for getting image drawable like this:
public static Drawable getDrawable(Context mContext, String name) {
int resourceId = mContext.getResources().getIdentifier(name, "drawable", mContext.getPackageName());
return mContext.getResources().getDrawable(resourceId);
}
if you have the image in the drawable folder you are going about this the wrong way.
try something like this
Resources res = getResources();
String mDrawableName = "logo_default";
int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID );
icon.setImageDrawable(drawable );
No need to use getDrawable() you directly use the resource id like
String mDrawableName = "myimageName";
int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName());
imgView.setImageResource(resID);
ImageView img = (ImageView) findViewById(R.id.{ImageView id});
img.setImageResource(getResources().getIdentifier("ImageName","drawable",getPackageName()));