智慧服务,成就美好体验 项目咨询

主页 > 服务与支持 > 开发平台 > 客户端SDK参考 > Android Native SDK > 通讯录 设置和获取联系人头像

入门使用

设置和获取联系人头像

更新时间:2019-11-20

描述

用户可以更改自己的头像。使用的头像可以是系统头像也可以是自定义头像也可以获取头像,在获取头像时服务器会推送获取头像的回调消息给用户,从服务器上得到指定联系人的头像信息。

前提条件

已完成组件的初始化和登录。

业务流程

  • 设置系统头像
    图1 设置系统头像流程 
    UI调用TsdkEAddrManager对象中的setSystemIcon()方法设置系统头像。
    说明: 
    • 参数iconId是指系统默认头像图片文件的图片名。图片名一般是数字。这些图片文件保存在用户的本地文件夹中。UI通过iconId找到对应的图片文件,加载到界面呈现。这些图片不需要从服务器上获取。
    • 禁止修改系统默认头像图片的文件名,以免造成iconId对应的图片文件与其他用户不一致,导致界面显示用户系统默认头像不一致。
    • 头像大小建议不要超过2M,否则可能影响UI性能。

    代码示例:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    //java code
    TsdkEAddrManager tsdkEAddrManager = TsdkManager.getInstance().geteAddrManager();
    public int setSystemIcon(int resId)
    {
        int result = tsdkEAddrManager.setSystemIcon(resId);
        if (result != 0)
        {
            Log.e(TAG, "Set user system icon filed, result -->" + result);
        }
        return result;
    }
    
     
  • 设置自定义头像
    图2 设置自定义头像流程 
    UI调用TsdkEAddrManager对象中的setUserDefIcon()方法设置自定义头像。
    说明: 
    • 设置自定义头像需注意:参数需要传入要设置头像的路径,并且头像需要选择为小头像52*52、中头像120*120和大头像320*320三种,其中具体大小应根据服务器要求来定。
    • 图片的格式不支持TIFF格式,推荐使用png格式。
    • 图片的大小服务器没有限制,产品可以根据实际的需要进行限制,建议图片大小最大值为2MB。
    • 若返回成功,则返回设置成功和修改时间,否则返回相应的错误码。

    代码示例:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    //java code
    TsdkEAddrManager tsdkEAddrManager = TsdkManager.getInstance().geteAddrManager();
    public int setDefinedIcon(String smallIconFilePath, String mediumIconFilePath, String largeIconFilePath)
    {
        TsdkIconInfo iconInfo = new TsdkIconInfo(smallIconFilePath, mediumIconFilePath, largeIconFilePath);
        String result = tsdkEAddrManager.setUserDefIcon(iconInfo);
    
        if (null != result)
        {
            return 0;
        }
    
        return -1;
    }
    
     
  • 获取联系人头像
    图3 获取联系人头像流程 
  1. UI调用TsdkEAddrManager对象中的getUserIcon()方法获取联系人头像。

     

    说明: 

    需要传入的参数为查询的帐户以及序列号。

    代码示例:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    //java code
    TsdkEAddrManager tsdkEAddrManager = TsdkManager.getInstance().geteAddrManager();
    private int queryContactsIconSeq = 1;
    private static Map<Integer, String>querySeqAccountMap = new HashMap<>();
    
    public int getUserIcon(String account)
    {
        int seq = queryContactsIconSeq++;
        TsdkGetIconParam iconParam = new TsdkGetIconParam();
        iconParam.setSeqNo(seq);
        iconParam.setAccount(account);
        querySeqAccountMap.put(seq, account);
        int result = tsdkEAddrManager.getUserIcon(iconParam);
        if (result != 0)
        {
            Log.e(TAG, "search user icon failed -->" + result);
        }
        return seq;
    }
    
     

     

  2. SDK通过TsdkNotify接口中的onEvtGetIconResult()方法向UI上报查询联系人头像事件,UI获得所查询的联系人头像内容。

     

    说明: 

    查询头像的结果信息包括操作结果,查询序号,系统头像ID(用户设置的是系统头像)或者自定义头像文件路径(用户设置的是自定义头像)。

    代码示例:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    //Java code
    public void handleGetIconResult(int querySeqNo, TsdkCommonResult result, TsdkGetIconResult getIconResult) {
        int ret = result.getResult();
        int seqNo = querySeqNo;
        String account = querySeqAccountMap.get(seqNo);
        if (ret == 0)
        {
            int sysId = getIconResult.getIconId();
            String avatarFile = getIconResult.getIconPath();
    
            if (querySelfIconSeq == seqNo)
            {
                EntAddressBookIconInfo selfIcon = new EntAddressBookIconInfo();
                selfIcon.setAccount(account);
                selfIcon.setIconFile(avatarFile);
                selfIcon.setIconId(sysId);
                selfIcon.setIconSeq(seqNo);
                notification.onEntAddressBookIconNotify(EntAddressBookConstant.Event.GET_SELF_ICON, selfIcon);
            }
            else if (sysId >= 0 && avatarFile.isEmpty())
            {
                EntAddressBookIconInfo iconInfo = new EntAddressBookIconInfo();
                iconInfo.setAccount(account);
                iconInfo.setIconId(sysId);
                iconInfo.setIconSeq(seqNo);
                notification.onEntAddressBookIconNotify(EntAddressBookConstant.Event.GET_CONTACTS_SYSTEM_ICON, iconInfo);
            }
            else
            {
                EntAddressBookIconInfo iconInfo = new EntAddressBookIconInfo();
                iconInfo.setAccount(account);
                iconInfo.setIconFile(avatarFile);
                iconInfo.setIconSeq(seqNo);
                notification.onEntAddressBookIconNotify(EntAddressBookConstant.Event.GET_CONTACTS_CUSTOM_ICON, iconInfo);
            }
            Log.i(TAG, sysId + "System Avatar ID  " + avatarFile + "Custom Avatar filename");
        }
        else
        {
            EntAddressBookIconInfo iconInfo = new EntAddressBookIconInfo();
            iconInfo.setAccount(account);
            iconInfo.setIconSeq(seqNo);
            Log.e(TAG, "User get icon failed, result -->" + result);
            notification.onEntAddressBookIconNotify(EntAddressBookConstant.Event.GET_CONTACTS_ICON_FAILED, iconInfo);
        }
    }
    
     

     

注意事项

无。