Android 如果我知道图像的名称,如何获取图像的资源 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3042961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the resource id of an image if I know its name?
提问by manu
How do I get the resource id of an image if I know its name (in Android)?
如果我知道图像的名称(在 Android 中),如何获取图像的资源 ID?
回答by Francesco Laurita
With something like this:
像这样:
String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
回答by VSB
You can also try this:
你也可以试试这个:
try {
Class res = R.drawable.class;
Field field = res.getField("drawableName");
int drawableId = field.getInt(null);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
I have copied this source codes from below URL. Based on tests done in this page, it is 5 times faster than getIdentifier(). I also found it more handy and easy to use. Hope it helps you as well.
我从下面的 URL 复制了这个源代码。根据本页中完成的测试,它比 getIdentifier() 快 5 倍。我还发现它更方便易用。希望它也能帮助你。
回答by naXa
Example for a public system resource:
公共系统资源的示例:
// this will get id for android.R.drawable.ic_dialog_alert
int id = Resources.getSystem().getIdentifier("ic_dialog_alert", "drawable", "android");
Another way is to refer the documentation for android.R.drawableclass.
另一种方法是参考android.R.drawable类的文档。
回答by Azhar
You can use this function to get a Resource ID:
您可以使用此函数获取资源 ID:
public static int getResourseId(Context context, String pVariableName, String pResourcename, String pPackageName) throws RuntimeException {
try {
return context.getResources().getIdentifier(pVariableName, pResourcename, pPackageName);
} catch (Exception e) {
throw new RuntimeException("Error getting Resource ID.", e)
}
}
So if you want to get a DrawableResource ID, you can call the method like this:
所以如果你想获得一个DrawableResource ID,你可以像这样调用方法:
getResourseId(MyActivity.this, "myIcon", "drawable", getPackageName());
(or from a fragment):
(或来自片段):
getResourseId(getActivity(), "myIcon", "drawable", getActivity().getPackageName());
For a StringResource ID you can call it like this:
对于字符串资源 ID,您可以这样称呼它:
getResourseId(getActivity(), "myAppName", "string", getActivity().getPackageName());
etc...
等等...
Careful: It throws a RuntimeException if it fails to find the Resource ID. Be sure to recover properly in production.
小心:如果找不到资源 ID,它会抛出 RuntimeException。确保在生产中正确恢复。
回答by sunil shah
One other scenario which I encountered.
我遇到的另一种情况。
String imageName ="Hello" and then when it is passed into getIdentifier function as first argument, it will pass the name with string null termination and will always return zero. Pass this imageName.substring(0, imageName.length()-1)
String imageName ="Hello" 然后当它作为第一个参数传递给 getIdentifier 函数时,它将传递带有字符串 null 终止的名称,并且将始终返回零。传递这个 imageName.substring(0, imageName.length()-1)