tags:

views:

800

answers:

2

I have:

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

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

+6  A: 

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
Thanks, after you gave me name of the method it was easy to find this page -> http://www.anddev.org/viewtopic.php?p=35661
kape123
+4  A: 

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);
pfleidi