0

I want to find the file path to an image stored in res/drawable/ in a format that is easy to parse. For example, drawable://2131099733 is bad to parse, but /drawable/1.jpg is easy. What I've tried below doesn't work, but that would be the ideal way to do it.

The reason why I want it is because I will have images that are named things like 1.jpg and 2.jpg, then I will be able to use string concatenation to easily load it into an ImageView.

File imgFile = new File("/res/drawable/adorable-animal-cat-20787.jpg");
Mike M.
  • 38,532
  • 8
  • 99
  • 95
Daniel
  • 23
  • 5
  • Resource drawables don't exist as files on the device. They are only files on your development machine. If you want to load a drawable by its name, the answers on the linked duplicate have examples of how to do that. – Mike M. Apr 29 '19 at 02:35

1 Answers1

0

This (using the File class) will never work because at runtime the drawables are packaged in the APK.

Now, to load a drawable into an ImageView at runtime (from code) simply do the following:

imageView.setBackgroundResource(R.drawable.drawable_resource_identifier);

However, you need to load the drawables from a String value you can use the Resources class to resolve the identifier of the drawable...

int resID = context.getResources().getIdentifier("drawable_name", "drawable", getPackageName());
imageView.setImageResource(resID);
Leo
  • 14,625
  • 2
  • 37
  • 55