How to get the Resource.Id from Value?

瘦欲@ 提交于 2019-12-21 21:36:41

问题


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:


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.




回答2:


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:


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


来源:https://stackoverflow.com/questions/7030395/how-to-get-the-resource-id-from-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!