Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何检查if子句中是否存在图像?_Android_Image_Android Drawable - Fatal编程技术网

Android:如何检查if子句中是否存在图像?

Android:如何检查if子句中是否存在图像?,android,image,android-drawable,Android,Image,Android Drawable,我需要知道如何检查R.drawable中是否存在图像,它必须是动态的,因此我必须使用一个字符串来提供图像的名称。 我试过了=“空”或“存在”,但它不起作用。。。。请帮帮我 titulo=bundle.getString("titulo"); textView = (TextView) findViewById( R.id.textView1); textView.setText(titulo); foto="f"+bundle.getString("numero")+"a"; System.ou

我需要知道如何检查R.drawable中是否存在图像,它必须是动态的,因此我必须使用一个字符串来提供图像的名称。 我试过了=“空”或“存在”,但它不起作用。。。。请帮帮我

titulo=bundle.getString("titulo");
textView = (TextView) findViewById( R.id.textView1);
textView.setText(titulo);
foto="f"+bundle.getString("numero")+"a";
System.out.println(foto);
flipper =(ViewFlipper) findViewById(R.id.vfFlipper);
这给了我需要的图像的名称

image = new ImageView(this);
image= new ImageView(this);
image.setImageResource(R.drawable.f00a1);
image.setScaleType(ScaleType.FIT_CENTER);
flipper.addView(image);
在这里,我可以使用图像,但我需要使用变量“foto”,这样它就可以是动态的
谢谢

您可以使用
getResources
获取
Resources
类的实例。在
Resources
类中,如果找不到资源,则会得到,这也意味着找不到图像

所以代码是这样的

Resource r = getResources();
Bool fileFound = true;
Drawable d = null;
try{
d = r.getDrawable(your_image_id);
}
catch(ResourceNotFoundException e){
fileFound = false;
}
if(findFound){
// Your operations
// set drawable to your imageview.
}

您可以使用
getResources
获取
Resources
类的实例。在
Resources
类中,如果找不到资源,则会得到,这也意味着找不到图像

所以代码是这样的

Resource r = getResources();
Bool fileFound = true;
Drawable d = null;
try{
d = r.getDrawable(your_image_id);
}
catch(ResourceNotFoundException e){
fileFound = false;
}
if(findFound){
// Your operations
// set drawable to your imageview.
}

R类中的所有内容都是整数-您不能创建一个字符串来表示资源id。最接近的方法是使用
getResources()
然后调用


…这将允许您根据资源名称查找表示资源的整数。

R类中的所有内容都是整数-您不能创建字符串来表示资源id。最接近的方法是使用
getResources()
然后调用


…这将允许您根据资源名称查找表示资源的整数。

谢谢大家!我把这两个答案混在一起,然后。。成功了

Resource r = getResources();
Bool fileFound = true;
Drawable d = null;
try{
d = r.getDrawable(getIdentifier(foto1, "drawable", getPackageName());
}
catch(ResourceNotFoundException e){
fileFound = false;
}
if(findFound){
// Your operations
// set drawable to your imageview.
}

谢谢大家!!我把这两个答案混在一起,然后。。成功了

Resource r = getResources();
Bool fileFound = true;
Drawable d = null;
try{
d = r.getDrawable(getIdentifier(foto1, "drawable", getPackageName());
}
catch(ResourceNotFoundException e){
fileFound = false;
}
if(findFound){
// Your operations
// set drawable to your imageview.
}
检查上述语句的非零值

if(0)
   //does not exists
 else
   //exists
检查上述语句的非零值

if(0)
   //does not exists
 else
   //exists

R.Drawable将只包含那些存在的图像。如果找不到该文件,它会在编译时发出抱怨。R.Drawable将只包含那些存在的图像。如果找不到该文件,它会在编译时抱怨。它可以工作!但是仍然不能引用我创建的文件名的图像id…它可以工作!但仍然无法使用我创建的文件名引用图像\u id…我尝试使用getResources(getIdentifier(“imagen1”))但没有成功!我错了吗?我试过使用int-RID=this.getApplicationContext().getResources().getIdentifier(foto1,null,getPackageName());d=r.可提取(RID);但是RID总是0…您需要使用
getIdentifier(foto1,“drawable”,getPackageName())
-第二个参数是资源类型。我尝试使用getResources(getIdentifier(“imagen1”)),但没有成功!我错了吗?我试过使用int-RID=this.getApplicationContext().getResources().getIdentifier(foto1,null,getPackageName());d=r.可提取(RID);但是RID总是0…您需要使用
getIdentifier(foto1,“drawable”,getPackageName())
-第二个参数是资源类型。