The method setImageDrawable(getResources().getDrawable(XX)) doesn't display the good image

Ask by : Ronandroid June 21, 2013 21:01

i succeed in using this code in a new project, but, when copying it in my actual code, the image displayed is not the good one everything is well made (image is in the folder drawable, every data well declared, ...) image begin with "phoXX", where XX is an integer (between 0 and 62) i have an imageview (named imageView1) and a TextView (to debug, named textView1) in which i display the ID (which one is good when checking in

here is my code :

private Context mContext;
ImageView  imageC=(ImageView)findViewById(;
TextView  Tex=(TextView)findViewById(;       
Drawable drawableX = this.getResources().getDrawable(android.R.drawable.ic_dialog_alert);
Random random_monster = new Random();
int lerand = random_monster.nextInt(62);
int id = mContext.getResources().getIdentifier("drawable/pho"+lerand, "drawable",mContext.getPackageName());
if (id>0)
    drawableX = getResources().getDrawable(id);

So, as i say, an image is displayed, a number is displayed, but pho29 for exemple show the image named pho48 (and pho48 doesn't display pho29 if you ask) ID is 2130837535, which is what appears in : public static final int pho29=0x7f02001f; pho48 is :
public static final int pho48=0x7f020034;

for some number, the image displayed is the good one, for other, the image is not the good one (and it's always the same image displayed for the same number....i mean for pho29, it's ALWAYS pho48 which is displayed, in every one of my activity)

I really don't understand what i miss...Thx all for any kind of help

Edit : below some matching (on the left side the number which should be displayed, on the right the number displayed)


As you can see, 56 is displayed by 56 AND 37....

View original question

Answer by : Mark RobertsNovember 03, 2012 21:20

If the same code and set of image resources work fine in one project and not in another, you should consider whether the compiled resources in the broken project are actually correct. I have found when using the automatic build feature in Eclipse that the resources don't get recompiled when I expect them to. For instance if the last thing I did before deploying an app was to change the resources. Saving a code file change seems to drive the build system harder.

You should try doing a clean then a rebuild. In Eclipse, on the Project menu choose "Clean..." and pick your project (or let it clean them all). Eclipse will then do a full rebuild. Or on the command line run "ant clean" followed by "ant debug".

Also you are using this line:

int id = mContext.getResources().getIdentifier("drawable/pho"+lerand, "drawable",mContext.getPackageName());

Here is the API spec:

public int getIdentifier (String name, String defType, String defPackage)

You don't need to specify the type in the name field, because you are specifying it in the defType field. I don't think it will solve your problem, but this should work at least as well:

int id = mContext.getResources().getIdentifier("pho"+lerand, "drawable",mContext.getPackageName());
View original answer