[SalesForce] Insert an image in Chatter Post

I want insert in a chatter post an image that i have uploaded. How can i reference this resource in my code?
I have different images loaded as static resources.
I have a chatter's post on profile of the user but the picture isn't loaded.

Most recent code:

   List<FeedItem> posts = new List<FeedItem>();
   List<user> lst=[Select id,name from user where date_of_birth__c =today];

    // search by static resource name
   List <StaticResource> resourceList = [SELECT Name, Body
    FROM StaticResource WHERE Name like '%Filehappy%'ORDER BY Name ASC];
      for(User u:lst)
      {
       //generate a random number [0-5] for resource's name. 
            Integer index=math.mod(Integer.valueof(Math.random()*100),6);
            Blob decodedbody=resourceList[index].body;
            FeedItem post = new FeedItem();
            post.ParentId = u.id;
            post.CreatedById='005E0000002cjXv';
            post.Body = 'Happy birthday to '+u.name+'!';
            post.ContentData = decodedbody;
            post.ContentFileName = 'Wishes.jpg!';
            posts.add(post);
      }
         insert posts;
}

Thank you very much!
KR

Best Answer

enter image description here

Blob body=[Select Name, ContentType, Body From StaticResource where name='abc'].body;

 FeedItem post = new FeedItem();
         post.ParentId = u.id;
         post.CreatedById='005E0000003XWRs';
         post.Body ='Happy birthday '+u.name;
         post.ContentData = body;
         post.ContentFileName = 'sample.png';
         insert post;

The above code i fetch data from static resource and use as a ContentData directly

http://wiki.developerforce.com/page/Chatter_Code_Recipes Refer these recepies .

Related Topic