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
Harish PHarish P 

How to Upload a Profile photo in base64 format For Community Users Using ConnectApi.UserProfiles.setPhoto

Am Uploading Profile Photo for Community Users in base64 format By Using ConnectApi.UserProfiles.setPhoto Method. But am getting "ConnectApi.ConnectApiException: The file you uploaded doesn't appear to be a valid image" This error, Help me to Fix this issue.
Raj VakatiRaj Vakati
You need to set like below
 
Blob blobValue =' blob values';
    String contentType ='content type'; 
String filename ='file name';
    
     Id userId 	='User Id';
ConnectApi.BinaryInput photoFileInput =	new ConnectApi.BinaryInput(blobValue, contentType, filename);
  ConnectApi.UserProfiles.setPhoto(Network.getNetworkId(), userId, photoFileInput);
 
public with sharing class ProfilePhotoUploadController {
	
	/*
     * The binary data of the image uploaded by the user
     */
    public transient Blob blobValue { get; set; }
    
    /*
     * The content type, determined by Salesforce, of
     * the image uploaded by the user
     */
    public transient String contentType { get; set; }
    
    /*
     * The name of the image file uploaded by the user
     */
    public transient String filename { get; set; }
    
    /*
     * The User ID of the community user for which
     * a new profile photo is being uploaded
     */
    public Id userId { get; set; }
    
    /*
     * @return the URL for the large profile photo of the specified
     *         user in the current community
     */
    public String getLargePhotoUrl() {
        return ConnectApi.UserProfiles.getPhoto(Network.getNetworkId(), userId).largePhotoUrl;
    }
    
    /*
     * @return the appropriate next page after setting the 
     *         specified user's profile photo to the new
     *         image uploaded
     */
    public PageReference upload() {
        
        if(blobValue != null)
		{
			ConnectApi.BinaryInput photoFileInput =	new ConnectApi.BinaryInput(blobValue, contentType, filename);
        
        	if(!Test.isRunningTest()) // This method cannot be tested and must be bypassed 
			{
				ConnectApi.UserProfiles.setPhoto(Network.getNetworkId(), userId, photoFileInput);
			}
		}		
        
        return null; // Leave the user on the current page
    }
}


 
Harish PHarish P

Same error am getting
Hi @Raj Vakati

Same error am Getting

Ephetehe MokgopanyaneEphetehe Mokgopanyane
Hi Harish

I know it is a year later however, please see solution below that I used.
public static Boolean updateUserProfilePic(String userProfilePicString, String userId, String fileType){
	Boolean updateSuccessful = true;
	
	System.debug('-------------' + userProfilePicString.length());
	try{
		Blob blobImage = EncodingUtil.base64Decode(userProfilePicString);
		
		ConnectApi.BinaryInput fileUpload = new ConnectApi.BinaryInput(blobImage, 'image/jpg', 'userImage.jpg');
		ConnectApi.Photo photoProfile = ConnectApi.UserProfiles.setPhoto(null, userId,  fileUpload);
	}
	catch(Exception exc){
		updateSuccessful = false;
	}
	
	return updateSuccessful;
}

 
Harsh Vardhana 40Harsh Vardhana 40

HI @Ephetehe Mokgopanyane 

Can you please show how to write test class for the same 

Ephetehe MokgopanyaneEphetehe Mokgopanyane
@Harsh Vardhana 40, Please see snipet below... 

@isTest(seeAllData=true)
    static void testUpdateUserProfilePicture(){
        String profilePicString = '/9j/4AAQSkZJRgABAQEAeAB4AAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAALAAgDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AL+AA//Z';
        Test.startTest();
        updateUserProfilePic(profilePicString,UserInfo.getUserId());
        Test.stopTest();
    }