简体   繁体   English

如何检查Android中是否存在资源

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

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.getResources().getIdentifier("my_resource_name", "drawable", mContext.getPackageName());
result = test != 0;

According to the javadoc you don't need the try catch: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29 根据javadoc你不需要try catch: http//developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String, %20java.lang.String 29%

if getIdentifier() returns zero, it means that no such resource exists. 如果getIdentifier()返回零,则表示不存在此类资源。
Also 0 - is an illegal resource id. 此外0 - 是非法资源ID。

So your result boolean variable is equivalent to (test != 0) . 所以你的结果布尔变量等价于(test != 0)

Anyway your try/finally is bad, because all it does it set the result variable to false even if exception is thrown from the body of try: mContext.get..... and then it just "rethrows" the exception after getting out of finally clause. 无论如何你的try / finally是坏的,因为所有这一切都将结果变量设置为false,即使从try的主体抛出异常: mContext.get.....然后它只是在出去后“重新抛出”异常最后一句。 And I suppose that is not what you want to do in case of exception. 而且我想这不是你想要做的例外情况。

The try/catch block in your code is totally useless (and wrong), since neither getResources() nor getIdentifier(...) throw an Exception. 代码中的try / catch块完全没用(和错误),因为getResources()getIdentifier(...)抛出异常。

So, getIdentifier(...) will already return you all you need. 因此, getIdentifier(...)已经为您提供所需的一切。 Indeed, if it will return 0, then the resource you are looking for does not exist. 实际上,如果它将返回0,那么您正在寻找的资源不存在。 Otherwise, it will return the associated resource identifier ( "0 is not a valid resource ID" , indeed). 否则,它将返回相关的资源标识符( “0确实不是有效的资源ID” )。

Here the correct code: 这里是正确的代码:

int checkExistence = mContext.getResources().getIdentifier("my_resource_name", "drawable", mContext.getPackageName());

if ( checkExistence != 0 ) {  // the resource exists...
    result = true;
}
else {  // checkExistence == 0  // the resource does NOT exist!!
    result = false;
}

In case someone is wondering, the "my_resource_name" in 万一有人想知道, "my_resource_name"

int checkExistence = mContext.getResources().getIdentifier("my_resource_name", "drawable", mContext.getPackageName());

is actually 实际上是

String resourceName = String.valueOf(R.drawable.my_resource_name);
int checkExistence = mContext.getResources().getIdentifier(resourceName , "drawable", mContext.getPackageName());

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 所以现在它不仅仅是为了绘画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM