Using setImageDrawable dynamically to set image in an ImageView
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView)findViewById(R.id.image1);
imgView.setImageDrawable(image);
or
setImageDrawable(getResources().getDrawable(R.drawable.icon));
The resource drawable names are not stored as strings, so you'll have to resolve the string into the integer constant generated during the build. You can use the Resources
class to resolve the string into that integer.
Resources res = getResources();
int resourceId = res.getIdentifier(
generatedString, "drawable", getPackageName() );
imageView.setImageResource( resourceId );
This resolves your generated string into the integer that the ImageView
can use to load the right image.
Alternately, you can use the id to load the Drawable
manually and then set the image using that drawable instead of the resource ID.
Drawable drawable = res.getDrawable( resourceId );
imageView.setImageDrawable( drawable );
You can try to use this code:
ImageView ImgView = (ImageView)findViewById(R.id.ImgView);
ImgView.setImageResource(R.drawable.myicon);
Try this,
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following
imageview.setImageResource(id);