ConnectApi makes out job easier, using connectAPi we can achieve this as follows

blob picContect = res.getBodyAsBlob();
// get content into picContent(of Blog Type)

ConnectApi.BinaryInput fileUpload = new ConnectApi.BinaryInput(picContect, 
'image/jpg', '<<Pic Name>>');
ConnectApi.Photo photoProfile = ConnectApi.ChatterUsers.setPhoto(<<communityId>>, userId, fileUpload);

Tada!!! thats it!

Input of Community Id goes likes this

  • Internal - **null**
  • Customer Community - **navigate to community and copy id from url**
  • Partner community - **navigate to community and copy id from url**