Issue
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");
Solution
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);
Answered By - Leo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.