Grab images from the drawable folder by name

Asked

Viewed 347 times

0

I would like to know how to take an image id from the drawable folder and use it in an Imageview. I am trying to do this within 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?

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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.