android中的R.id.image字符串

android中的R.id.image字符串,android,image,filenames,Android,Image,Filenames,在我的项目中,我试图通过编程操作其中一个图像的文件名来显示多个图像文件 例如,我可能有: filename.jpg、filename_top.jpg、filename_middle.jpg 我接收到一个可绘制int的输入,并试图在操作该文件名并试图显示程序生成的文件名之前找到显示图像的文件名。。问题是操纵的文件名不显示。 也就是说,这有点问题: imageView2.setImageResource(getImageId(this,namebooth)) 您知道如何修改getImageId以使s

在我的项目中,我试图通过编程操作其中一个图像的文件名来显示多个图像文件

例如,我可能有: filename.jpg、filename_top.jpg、filename_middle.jpg

我接收到一个可绘制int的输入,并试图在操作该文件名并试图显示程序生成的文件名之前找到显示图像的文件名。。问题是操纵的文件名不显示。 也就是说,这有点问题: imageView2.setImageResource(getImageId(this,namebooth))

您知道如何修改getImageId以使setImageResource正常工作吗

代码如下所示:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Bundle bun = getIntent().getExtras();
int imagenumber = bun.getInt("imagenumber");
String extension = bun.getString("extension");
// int become a val from 0 to 20 (array size)
setContentView(R.layout.clickeditem);

final int[] imgIds = new int[{
R.drawable.image0,R.drawable.image1,R.drawable.image2,,,R.drawable.image20};

//The first image with id top in the layout is set ok:
ImageView imageView1 = (ImageView)findViewById(R.id.top);
imageView1.setImageResource(imgIds [ imagenumber ] );
// problem here:
//try to get the name of this file: ie: filename.jpg
//       and then manipulate the filename:
String name = imageView1.getResources().getString(R.id.image0); 
//try to convert this to the filename_middle.jpg         
String namemiddle = name.replace(".jpg", "_middle.jpg");
imageViewt.setImageResource(getImageId(this, namemiddle)); 

//try to convert this to filename_bottom.jpg
String namebottom = name.replace(".jpg", "_bottom.jpg");
imageView2.setImageResource(getImageId(this, namebottom)); 
}

//where getImageId is defines as follows:
public static int getImageId(Context context, String imageName) 
{
return context.getResources().getIdentifier("drawable/" + imageName,
 null, context.getPackageName());
}
替换为

return context.getResources().getIdentifier(imageName,
 "drawable", context.getPackageName());

您好,谢谢您的回复,这有助于我理解标识符和标签。然而,仍然存在一个问题。。我上面的代码的错误在于我将.jpg传递给函数
int return_val=context.getResources().getIdentifier(“drawable/”+imageName,null,context.getPackageName())工作正常,但要求不提供文件名。(即imagename=filename\u top filename\u middle等。而不是filename\u top.jpg等。调用如下:
imageViewm.setImageResource(getImageId(this,namem));
希望对其他人有所帮助。
return context.getResources().getIdentifier(imageName,
 "drawable", context.getPackageName());