您是否在XML文件中声明了图像的ID?如果您这样做,您可以使用以下方法:
假设你的res / drawable文件夹中有picture.png.
在您的活动中,您可以在main.xml文件中设置图像资源
在FirstActivity中
//to retrieve image using id set in xml.
String imageString = "imageId"
int resID = getResources().getIdentifier(imageString , "id", "package.name");
ImageView image = (ImageView) findViewById(resID);
imageString是动态名称.之后,您可以获取动态资源的标识符.
另一种方法,你可以这样做:
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
您将能够引用您的图像资源并将ImageView设置为它.