开发者

How to get the Resource.Id from Value?

开发者 https://www.devze.com 2023-03-28 00:46 出处:网络
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 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.


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.


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());


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()));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号