2

I have a web service where I read various information from. One of these pieces of information is the name of an image. I have all the images stored locally in the drawables folder. What I have done right now is I read the string of the image I need from the web service. I want to use this string name and insert the image with the same string name from my drawable folder. Is this possible to do?

Is it possible to do the following somehow?

//Setup the image
        ImageView spotIvIcon = (ImageView)findViewById(R.id.spot_selected_image);
        String temp = "R.drawable."+spotImage;
        spotIvIcon.setImageResource(R.drawable.beer);

What I do with the temp string. Is it possible to convert this string into the int I need? I have the beer in there just to test something out. Thanks

skoko
  • 1,431
  • 2
  • 12
  • 13

2 Answers2

6

So I found out how to do this. If anyone wanted to know how it's done use the following code:

int path = getResources().getIdentifier(spotImage, "drawable", "com.androidpeople.tab");
        spotIvIcon.setImageResource(path);

Where:

  • spotImage is the name of your string
  • drawable is the type you want
  • com.androidpeople.tab is your package name

Works like a charm now.

skoko
  • 1,431
  • 2
  • 12
  • 13
2

you could just use the assets directory to store these images, and then load them using something like BitmapFactory.decodeStream(assetManager.open(imAnAssetName));

the asset directory it's here to be used for things you don't want to be managed (or that can't be managed) by the resource manager.

http://developer.android.com/reference/android/content/res/AssetManager.html .

RedBeard
  • 86
  • 9