function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tanner RussellTanner Russell 

Get image path from static resource with Apex

I am building a dynamic html email with apex and want to store all of my images in a zipped static resource. How can I pull the image path / reference from the static resource to use in my string.

Currently I have:
public static StaticResource static_resource = [SELECT Id,NamespacePrefix, name, SystemModStamp
                                          FROM StaticResource 
                                          WHERE Name = 'banner'
                                          LIMIT 1];

      String prefix = static_resource.NamespacePrefix;
        if( String.isEmpty(prefix) ) {
            prefix = '';
        } else {
            //If has NamespacePrefix
            prefix += '__';
       String url_file_ref = '/resource/'
                            + String.valueOf(((DateTime)static_resource.get('SystemModStamp')).getTime())
                            + '/' + prefix +
                            + static_resource.get('Name')
                            + '/image.png';

string lookingForURL = Url.getSalesforceBaseUrl().toExternalForm() +url_file_ref();

but the path is not working as a reference.
VinayVinay (Salesforce Developers) 
Hi Tanner,

You can try below.
String imageURL = URL.getSalesforceBaseUrl().toExternalForm() + PageReference.forResource('resourceName').getUrl();
System.debug('Here is image url:'+ imageURL );

Please mark as Best Answer if above information was helpful.