user678229
user678229

Reputation: 301

res.getIdentifier - find packagename

I need to display an image which I save under res/drawable folder. I get the image name from a web service call and I try to find this image in the drawable folder using the following code

ImageView backgroundImage = (ImageView) findViewById(R.id.ivFormBackgroundImage);
Resources res = getResources();
String mDrawableName = formSummary.backgroundImageName;
int resID = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID );
backgroundImage.setImageDrawable(drawable );

The resID always returns 0 and i wonder if it is because of the packagename. I have set my package name to be package="com.x.y.z.classes.ui.screens" in the Android_Manifest file, but the image is in res/drawable. Any help on how to get the path of the image is much appreciated. thanks...

Upvotes: 1

Views: 1358

Answers (2)

pents90
pents90

Reputation: 1792

It looks like you are doing it right, but make sure that the package name you specify matches the package name where the actual R class resides.

Upvotes: 2

ahodder
ahodder

Reputation: 11439

If you want to retrieve a drawable, your id should look similar to R.drawable.mydrawable

Upvotes: 0

Related Questions