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.

chobo2
  • 83,322
  • 195
  • 530
  • 832

3 Answers3

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 monodroid@lists.ximian.com subscribers, see the How to use Resources.GetIdentifier() thread, and the followup message.

jonp
  • 13,512
  • 5
  • 45
  • 60
  • 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" – Narcís Calvet Apr 17 '12 at 08:18
  • What if I need to access something like `com.android.internal.R.string.ime_action_done`? – IgorGanapolsky Dec 15 '15 at 19:56
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());

gtcompscientist
  • 671
  • 5
  • 18
  • No that seems to not have done anything. Still getting zero printed out. – chobo2 Aug 11 '11 at 17:58
  • 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? – gtcompscientist Aug 11 '11 at 18:06
  • 1
    Make sure you do not use "Resources.getSystem().getIdentifier(name, defType, defPackage)" but use the Resources object from current Context. – Sander Versluys Jul 08 '13 at 13:09
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()));
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85