2

I have a image called(my_image.png) in my Drawable-mdpi folder.

my android application interacts with a webservice. It might send back "my_image". My android application should load up this image.

I am using Monodroid and I tried this

 int abc = Resources.GetIdentifier("my_image","drawable",null); 

Yet the result is always "0". When it should be(from the resource file)

 // aapt resource value: 0x7f020000 public const int my_image = 2130837504; 

Looking around and the android way seems to be similar

int i = this.getResources().getIdentifier("txt_asecondtext", "strings", this.getPackageName()); 

I tried passing in the package name instead of null but that did nothing.

1
  • I believe I figured out why your last example line isn't working. strings should be string Commented Aug 11, 2011 at 18:15

3 Answers 3

5

The problem is twofold:

  1. You need to provide a package name in the Resources.GetIdentifier() call, and not use null.
  2. You need to use the correct package name.

The simple way to ensure you get the correct package name is to use the Android.Content.Context.PackageName property:

int id = Resources.GetIdentifier ("my_image", "drawable", PackageName); 

If you don't want to/can't use Context.PackageName, then look at the build output, in e.g. the obj\Debug\android\AndroidManifest.xml file, and use the value of the /manifest/@package attribute value. For example, AndroidManifest.xml may contain:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="MonoAndroidApplication2.MonoAndroidApplication2"> <!-- ... --> </manifest> 

Thus, you could instead use:

int id = Resources.GetIdentifier ("my_image", "drawable", "MonoAndroidApplication2.MonoAndroidApplication2"); 

For [email protected] subscribers, see the How to use Resources.GetIdentifier() thread, and the followup message.

Sign up to request clarification or add additional context in comments.

2 Comments

I found that using a package name as you did ("MonoAndroidApplication2.MonoAndroidApplication2") didn't work. I had to change it two 2 different substrings, eg.: "MonoForAndroid.MonoAndroidApplication2"
What if I need to access something like com.android.internal.R.string.ime_action_done?
3

You just need to format your request properly:

Instead of:

int i = this.getResources().getIdentifier("txt_asecondtext", "strings", this.getPackageName()); 

Try:

int i = getResources().getIdentifier("[fully qualified package]:drawable/[resource name]", null, null); 

So for Resource "my_image" in package "com.example" it would look like:

int i = getResources().getIdentifier("com.example:drawable/my_image", null, null); 

UPDATE: I also tested that the following works form me (including the log lines that prove it:

int i = getResources().getIdentifier( getPackageName() + ":drawable/" + resource_name, null, null); Log.d("EXAMPLE", "Just making sure that these IDs match: '" + i + "' == '" + R.drawable.resource_name + "'."); 

This can also be formatted like you did above, which I believe I have pointed out your error: int i = getResources().getIdentified(resource_name, "drawable", getPackageName());

3 Comments

No that seems to not have done anything. Still getting zero printed out.
I just tested this in my own code and it worked, so let's make sure that we have it configured the same way. Can you update your question with the line you are now trying?
Make sure you do not use "Resources.getSystem().getIdentifier(name, defType, defPackage)" but use the Resources object from current Context.
1

For a string ressource I do like this:

String s = "nameToFetch"; String text = getString(getResources().getIdentifier("str_" + s, "string", getPackageName())); 

So I think for your drawable you should call:

String s = "nameToFetch"; Drawable drawable = getDrawable(getResources().getIdentifier("d_" + s, "drawable", getPackageName())); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.