Pick images from drawable folder by name

0

I would like to know how to get the id of an image from the drawable folder and use it in an ImageView. I'm trying to do this inside a ListView

String nomeImgFilme = filme.getImagem();
int imgID = convertView.getResources().getIdentifier(nomeImgFilme, "drawable", context.getPackageName());

I'm trying to run this code, but it gives error:

  

"Resources $ NotFoundException: Resource ID # 0x7f060054".

And if I put the image directly with "R.drawable.circulo_fogo", it also says that it was not found.

So how do I get the id of an image by name inside a ListView?

    
asked by anonymous 08.04.2018 / 15:16

1 answer

0

You can use this code to capture the id from the drawable name

Resources resources = context.getResources();
int resourceId = resources.getIdentifier("minha_imagem_1", "drawable", 
   context.getPackageName());
imageview.setImageResource(resourceId);

I hope it helps!

    
09.04.2018 / 15:03