0

I tried to do it through "string" but it doesn't work. How to do it?

var TestString : String = "test1.png"
var TestId : Int = R.drawable.TestString
ATP
  • 2,939
  • 4
  • 13
  • 34

1 Answers1

0

You can do it this way:

val resId = applicationContext.resources.getIdentifier("test1", "drawable", applicationContext.packageName) 

yourImageView.setImageResource(resId)
Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
  • I have a lot of images with names like "test1, test2, .." and need to process them. I know that I need the ID of each image, but how do I get them from the filenames? – RealyJuniorGuy Jul 06 '22 at 10:49
  • I showed you how to pull a single one. Similarly, maybe you can for loop and solve it. I'm not sure about that. @RealyJuniorGuy – Halil Ozel Jul 06 '22 at 10:57