Hello Friends, In this tutorial, I'll show you, how you can read the image URL that has been inserted in the rich text area on sobject. In this unit, you can find a sample code that read the image URL from the account object. This image URL will be used to display the image on the visualforce page. So without wasting time, let's get started

Get image from the rich text area

Step 1: Login to your Salesforce Org. and open developer console.

Step 2: Navigate to File | New | Apex Class and Create an Apex controller called GetAccountImageHandler and replace with the following code.


GetAccountImageHandler.apxc
 public class GetAccountImageHandler {
    private Id id;
    public GetAccountImageHandler(){
        id = apexpages.currentpage().getparameters().get('id');
    }
    
    public string accountImageURL{
        get{
            String imageURL='';
            List<Account> lstAcc=[SELECT AccountImage__c FROM Account WHERE Id=:id];
            Matcher imgMatcher = Pattern.compile( '<img(.+?)>' ).matcher(lstAcc[0].AccountImage__c);           
            while (imgMatcher.find()) {                
                String imageTag = imgMatcher.group();
                System.debug('imageTag^^'+imageTag );              
                
                imageURL= imageTag.substringBetween(' src="', '"' );
                System.debug('imageURL^^' + imageURL );
            }
            return imageURL.unescapeHtml4();
        }
        set{}
    }
 }
Step 3: Navigate to File | New | Visualforce Page and create a new Visualforce Page called GetAccountImage and replace the following markup.

GetAccountImage.vfp
 <apex:page controller="getAccountImageHandler" sidebar="false">
    <img src="{!accountImageURL}"/>
 </apex:page>
Note: Place an account Id(Id=AccountId) as a parameter in URL after previewing this visualforce page.

Output:



See also:

Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.