0

1上午使用 ConnectApi.UserProfiles.setPhoto 方法以 base64 格式上传社区用户的个人资料照片。但是我收到“ConnectApi.ConnectApiException:您上传的文件似乎不是有效图像”这个错误,帮我解决这个问题。

4

2 回答 2

0

Hi you can try the below method:

public PageReference upload() {
    Blob b;
    document.AuthorId = UserInfo.getUserId();
    document.FolderId = UserInfo.getUserId(); // put it in running user's folder
    try {
        document.type = 'jpg';
        document.IsPublic = true;
        insert document;
        // ImageId = '06990000001HnuB';
        b = document.Body;
        //ConnectApi.ChatterUsers newPhoto = new ConnectApi.ChatterUsers();
    } catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error uploading file'));
        return null;
    } finally {
        document.body = null; // clears the viewstate
        document = new Document();
    }
    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'File uploaded successfully : ' + b));
    String communityId = null;
    String userId = UserInfo.getUserId();
    //ID fileId = ImageId;
    // Set photo
    ConnectApi.Photo photo = ConnectApi.ChatterUsers.setPhoto(communityId, userId, new ConnectApi.BinaryInput(b, 'image/jpg', 'userImage.jpg'));
    return null;
}
于 2019-02-17T06:12:55.453 回答
0

我遇到了同样的错误,没有太多细节,所以我不太确定你的问题。我使用下面的代码解决了这个问题。根据需要进行修改。

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;

}

于 2020-03-05T09:35:54.873 回答