How do I check to see if a resource exists in Android

前端 未结 4 1674
北恋
北恋 2020-12-01 07:23

Is there a built in way to check to see if a resource exists or am I left doing something like the following:

boolean result;
int test = mContext.getResource         


        
4条回答
  •  無奈伤痛
    2020-12-01 07:56

    i like to do something like that:

    public static boolean isResource(Context context, int resId){
            if (context != null){
                try {
                    return context.getResources().getResourceName(resId) != null;
                } catch (Resources.NotFoundException ignore) {
                }
            }
            return false;
        }
    

    so now it's not only for drawable

提交回复
热议问题