+ Start a Discussion

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;