In case anyone else comes across the problem I had, I was loading the image name from JSON and then needed to get the identifier. The second parameter of getIdentifier can be set to "drawable". This is a slight variation on @alia-ramli-ramli answer...
String uri = details.getString("circle").toLowerCase();
int imageResource = context.getResources().getIdentifier(uri, "drawable", context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
viewHolder.unit_circle.setImageDrawable(image);
where placeholder_image is the id of the resource. In your case, R.drawable.myresource.
Context can be an activity or the application. Pretty much wherever you are has reference to the context, which you can use to get the application's context.
In case anyone else comes across the problem I had, I was loading the image name from JSON and then needed to get the identifier. The second parameter of getIdentifier can be set to "drawable". This is a slight variation on @alia-ramli-ramli answer...
First, don't do that, as that
@drawable
syntax is meaningless in Java code. Useint resourceId=R.drawable.myresource
.If for some reason you do wind up a resource name and need the integer ID, use
getIdentifier()
on theResources
object.You can check here
you can also retrieve image from mipmap by replacing "drawable" with "mipmap" parameter of getIdentifier() method.
you can also add your own variable.. in my case scene.name between i followed @pfleidi answers. c stands for context.
I had the same problem with ... the deprecated etc. etc. I solved in such a way with minimum API 14 okay.
Long since overdue, but my favorite is to use the Context:
where placeholder_image is the id of the resource. In your case,
R.drawable.myresource
.Context can be an activity or the application. Pretty much wherever you are has reference to the context, which you can use to get the application's context.