Set image dynamically by retrieving image name from a data source in android...

    Sometimes there may be situation,where we have the images in our project's drawable folder.But we have to set the images by getting the image name from a data source.In normal condition we can use "R.drawable.imageName"  and the image is set. But in this as we get the image name at run time,we have to set the images dynamically.
     So in this situation first we have to retrieve the image name from the data source(It can be a SQLite database or any other data source).Store it in a string variable.Note here,we have to store  the image name here(not with the extension).So if the data source gives you image name with extension then cut the name from the extension.We need "imageName" here,not "imageName.png".So follow the code below...
try {                
                Class<drawable> res = R.drawable.class;
                if(str!=null){                                    
                Field field = res.getField(str);
                int drawableId = field.getInt(null);
                bengalidaypng.setImageResource(drawableId);               
                }
            }
            catch (Exception e) {
                      System.out.println("Image not found in drawable folder");
            } 
         Here "str" is the string variable that hold the imageName retrieved from a data source.If the image with desired name not found in the project's drawable folder then the "catch" block will execute.Hope it is very easy to understand.Feel free to make a comment if you have a doubt.