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

时间:2023-01-25 07:12:47

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;

4 个解决方案

#1


47  

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.
Also 0 - is an illegal resource id.

如果getIdentifier()返回0,则意味着不存在这样的资源。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. And I suppose that is not what you want to do in case of exception.

无论如何,您的try/finally都是糟糕的,因为它所做的一切都将结果变量设置为false,即使try: mContext.get的主体抛出异常。然后它只是在退出最后一个子句后“重新抛出”异常。我想这不是你想要做的以防例外。

#2


23  

The try/catch block in your code is totally useless (and wrong), since neither getResouces() nor getIdentifier(...) throw an Exception.

代码中的try/catch块是完全无用的(和错误的),因为getResouces()和getIdentifier(…)都没有抛出异常。

So, getIdentifier(...) will already return you all you need. Indeed, if it will return 0, then the resource you are looking for does not exist. Otherwise, it will return the associated resource identifier ("0 is not a valid resource ID", indeed).

因此,getIdentifier(…)将会返回你所需要的所有信息。实际上,如果它将返回0,则您正在查找的资源不存在。否则,它将返回相关的资源标识符(“0不是有效的资源ID”)。

Here the correct code:

正确的代码如下:

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

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

#3


3  

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

#4


1  

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

现在它不仅仅是用于绘制

#1


47  

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.
Also 0 - is an illegal resource id.

如果getIdentifier()返回0,则意味着不存在这样的资源。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. And I suppose that is not what you want to do in case of exception.

无论如何,您的try/finally都是糟糕的,因为它所做的一切都将结果变量设置为false,即使try: mContext.get的主体抛出异常。然后它只是在退出最后一个子句后“重新抛出”异常。我想这不是你想要做的以防例外。

#2


23  

The try/catch block in your code is totally useless (and wrong), since neither getResouces() nor getIdentifier(...) throw an Exception.

代码中的try/catch块是完全无用的(和错误的),因为getResouces()和getIdentifier(…)都没有抛出异常。

So, getIdentifier(...) will already return you all you need. Indeed, if it will return 0, then the resource you are looking for does not exist. Otherwise, it will return the associated resource identifier ("0 is not a valid resource ID", indeed).

因此,getIdentifier(…)将会返回你所需要的所有信息。实际上,如果它将返回0,则您正在查找的资源不存在。否则,它将返回相关的资源标识符(“0不是有效的资源ID”)。

Here the correct code:

正确的代码如下:

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

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

#3


3  

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

#4


1  

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

现在它不仅仅是用于绘制