How to get Resource.Id from value?

I have an image named (my_image.png) in my Drawable-mdpi folder.

my android app is interacting with a web service. It can send back "my_image". My Android app should download this image.

I use Monodroid and I tried this

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

However, the result is always "0" . When will it be (from the resource file)

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

Looking around, and it looks like an android looks like

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

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

+2
source share
3 answers

The problem is twofold:

  • You need to specify the package name in the call to Resources.GetIdentifier() , and not use null .
  • You need to use the correct package name.

An easy way to provide the correct package name is the Android.Content.Context.PackageName property:

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

If you do not want / cannot use Context.PackageName , then look at the output of the assembly, for example. obj\Debug\android\AndroidManifest.xml , and use the value of the /manifest/@package attribute. 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> 

So you could use:

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

For subscribers of monodroid@lists.ximian.com see How to use thread.GetIdentifier () , and the subsequent message .

+5
source

You just need to format your request correctly:

Instead:

 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 the package "com.example" it will look like this:

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

UPDATE: I also tested that the following works to change (including the log lines that prove this:

 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 + "'."); 

It can also be formatted, as you did above, and I believe that I pointed out your error: int i = getResources (). getIdentified (resource name, "drawable", getPackageName ());

+3
source

For the ressource line, I like the following:

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

So, I think you should call for your convenience:

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

Source: https://habr.com/ru/post/1205178/


All Articles