Tom
Tom

Reputation: 3627

Android getResources/getIdentifier not returning ID

I have code like tthis:

String s = "replace__menu__" + data.imageid + ".png";
int RID = this.getApplicationContext().getResources().getIdentifier(s, "drawable-hdpi", getPackageName());

The String s = instruction sets a value that is the same as one of names in my res/drawable-hdpi folder. However, the returned value sets RID to the value 0

Any idea why my code is not working? Am I doing something wrong?

Upvotes: 3

Views: 5275

Answers (2)

Teovald
Teovald

Reputation: 4389

".png" is not part of a ressource name
"drawable-hdpi" I would try just 'drawable' instead 

Upvotes: 3

Pragnani
Pragnani

Reputation: 20155

Try this

String s = "replace__menu__" + data.imageid;  // image name is needed without extention
int RID = this.getApplicationContext().getResources().getIdentifier(s, "drawable", getPackageName());

Upvotes: 6

Related Questions