从动画中获取图像

时间:2017-03-21 20:44:26

标签: android imageview

我正在使用条形码阅读器应用程序,我想在条形码读取时显示产品图像,因此我将产品条形码编号重命名为照片并放入drawables文件夹。照片名称适用于ex 1234567,尝试使用barcodeNo进行检索。但我在条形码上没有错误。我怎么能这样做?

ImageView iv = (ImageView) findViewById(R.id.imageView1);   
iv.setImageResource(R.drawable.barcodeNo);

3 个答案:

答案 0 :(得分:0)

只需在getImage方法中传递代码,即可从drawable文件夹中找到您的图像。

iv.setImageResource(getImage(barcodeNo));

public int getImage(String imageName) {

     int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());

     return drawableResourceId;
}

答案 1 :(得分:0)

@narendra Sorathiya回答是对的。你已经编写了这段代码来解决你在屏幕截图上发布的NullPointerException

ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(getImage(barcodeNo));
.
.
.
.
public int getImage(String imageName) {

     int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());

     return drawableResourceId;
}

答案 2 :(得分:0)

String uri = "@drawable/myresource";  // where myresource (without the extension) is the file

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);
OR
imageview.setImageResource(imageResource)