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
jucuzoglujucuzoglu 

How do I create a Base64Encoded string of an image in Apex?

I want to grab an Image, accessed via a URL (or it could be a StaticResource) turn it into a Base64Encoded Blob so that I can attach it to a Chatter FeedItem (post).

 

Here is my (incorrect) attempt trying to accomplish this (using a PDF instead of an image) which happens to be timing out.

 

//Adding a Content post
PageReference p = new PageReference('http://www.stluciadance.com/prospectus_file/sample.pdf');
Blob body = p.getContent();
FeedItem post = new FeedItem();
post.ParentId = '0F9800000004FIF';
post.Body = 'This is sample text #3 with attachment';
post.CreatedById = '00580000003frfk';
post.ContentData = body;
post.ContentFileName = 'SomeFile.pdf';
insert post;