如果知道一个图像的名称,如何获得它的资源id ?

时间:2022-01-30 00:32:53

How do I get the resource id of an image if I know its name (in Android)?

如果我知道图像的名称(在Android中),如何获得它的资源id ?

5 个解决方案

#1


251  

With something like this:

是这样的:

String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

#2


26  

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倍。我还发现它更方便、更容易使用。希望它对你也有帮助。

Link: Dynamically Retrieving Resources in Android

链接:在Android中动态检索资源。

#3


10  

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

如果知道一个图像的名称,如何获得它的资源id ?

Another way is to refer the documentation for android.R.drawable class.

另一种方法是参考android.R的文档。可拉的类。

#4


9  

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 Drawable Resource ID, you can call the method like this:

如果你想获得一个可绘制的资源ID,你可以这样调用方法:

getResourseId(MyActivity.this, "myIcon", "drawable", getPackageName());

(or from a fragment):

(或片段):

getResourseId(getActivity(), "myIcon", "drawable", getActivity().getPackageName());


For a String Resource 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,它会抛出一个运行时异常。请确保在生产环境中正确恢复。

Read this

读到这

#5


0  

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)

字符串imageName ="Hello",然后当它作为第一个参数传递给getIdentifier函数时,它将传递带有字符串null终止的名称,并且总是返回0。通过这次imageName。substring(0,imageName.length()1)

#1


251  

With something like this:

是这样的:

String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

#2


26  

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倍。我还发现它更方便、更容易使用。希望它对你也有帮助。

Link: Dynamically Retrieving Resources in Android

链接:在Android中动态检索资源。

#3


10  

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

如果知道一个图像的名称,如何获得它的资源id ?

Another way is to refer the documentation for android.R.drawable class.

另一种方法是参考android.R的文档。可拉的类。

#4


9  

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 Drawable Resource ID, you can call the method like this:

如果你想获得一个可绘制的资源ID,你可以这样调用方法:

getResourseId(MyActivity.this, "myIcon", "drawable", getPackageName());

(or from a fragment):

(或片段):

getResourseId(getActivity(), "myIcon", "drawable", getActivity().getPackageName());


For a String Resource 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,它会抛出一个运行时异常。请确保在生产环境中正确恢复。

Read this

读到这

#5


0  

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)

字符串imageName ="Hello",然后当它作为第一个参数传递给getIdentifier函数时,它将传递带有字符串null终止的名称,并且总是返回0。通过这次imageName。substring(0,imageName.length()1)