63

I have:

String uri = "@drawable/myresource.png";

How can I load that in ImageView? this.setImageDrawable?

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
nikib3ro
  • 20,366
  • 24
  • 120
  • 181

7 Answers7

172

If you really need to work with a string, try something like this:

private void showImage() {
    String uri = "drawable/icon";

    // int imageResource = R.drawable.icon;
    int imageResource = getResources().getIdentifier(uri, null, getPackageName());

    ImageView imageView = (ImageView) findViewById(R.id.myImageView);
    Drawable image = getResources().getDrawable(imageResource);
    imageView.setImageDrawable(image);
}

Else I would recommend you to work with R.* references like this:

int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Ziem
  • 6,579
  • 8
  • 53
  • 86
pfleidi
  • 2,936
  • 3
  • 19
  • 10
  • 2
    I was loading the image name from some JSON, the URI didn't contain "drawable/" at the start and I wasn't getting a resource. In this case you can set the second parameter to `getIdentifier()` to "drawable" – Flexicoder Oct 26 '14 at 12:56
  • keep in mind using it that android studio see it unused resource so "refactor/remove unused resources" option not work for it – gturedi Jan 23 '17 at 13:02
  • 1
    Remember that you must don't add image extension to URI and must add "drawable/" to your first of URI – Mahdi Astanei Feb 06 '17 at 09:17
25

First, don't do that, as that @drawable syntax is meaningless in Java code. Use int resourceId=R.drawable.myresource.

If for some reason you do wind up a resource name and need the integer ID, use getIdentifier() on the Resources object.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 4
    Thanks, after you gave me name of the method it was easy to find this page -> http://www.anddev.org/viewtopic.php?p=35661 – nikib3ro Feb 28 '10 at 02:15
11

you can also add your own variable.. in my case scene.name between i followed @pfleidi answers. c stands for context.

String uri = "drawable/" + scene.name; 
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());

imageList.setImageResource(imageResource);
Ziem
  • 6,579
  • 8
  • 53
  • 86
Alia Ramli Ramli
  • 405
  • 6
  • 18
6

Long since overdue, but my favorite is to use the Context:

context.getApplicationContext().getResources().getDrawable(R.drawable.placeholder_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.

Garg
  • 2,731
  • 2
  • 36
  • 47
Wendel
  • 71
  • 1
  • 6
  • pfleidi's response is accessing the same method - Activities are Contexts, and so when he calls getResources(), he's using the current Activity as a context to get to the same place you did. Edit: not quite the same, you're getting to the global Application context rather than using the Activity (or Service) context directly. – MaximumGoat Dec 01 '11 at 19:49
4

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);
Ziem
  • 6,579
  • 8
  • 53
  • 86
Flexicoder
  • 8,251
  • 4
  • 42
  • 56
4

You can check here

int resID = mContext.getResources().getIdentifier(stringfilename, "drawable", mContext.getPackageName());
iv_imageview.setImageDrawable(ContextCompat.getDrawable(mContext,resID)); 

you can also retrieve image from mipmap by replacing "drawable" with "mipmap" parameter of getIdentifier() method.

Sanjay Bhalani
  • 2,424
  • 18
  • 44
0

I had the same problem with ... the deprecated etc. etc. I solved in such a way with minimum API 14 okay.

...
...
ImageView imgPoster = viewCache.getImgPoster(resource);
String uri = "drawable/" + film.getPoster();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable image = ContextCompat.getDrawable(context, imageResource);
imgPoster.setImageDrawable(image);

return convertView;
...
...
Maurizio
  • 9
  • 1