附件类文件存储在环信和不存储在环信时的两种实现方式

场景一: 附件类文件存储在环信服务器

使用环信EMChatManager#downloadAttachment下载附件方案
(本篇文章以图片消息为例,其他附件类消息类似):

一、 通过EMFileMessageBody#getLocalUrl判断有没有本地文件;


EMImageMessageBody imgBody = (EMImageMessageBody) message.getBody();
      //本地文件的资源路径 
      String imageLocalUrl = imgBody.getLocalUrl();

在这里插入图片描述
1 、 如果存在本地路径,直接使用本地文件;(本案例使用的Glide)

      Glide.with(this).load(imageLocalUrl).into(image);

2. 如果不存在本地文件,调用EMChatManager#downloadAttachment下载,下载成功后展示图片;(本案例使用的Glide)

EMMessage msg = EMClient.getInstance().chatManager().getMessage(msgId);
    EMCallBack callback = new EMCallBack() {
    
    
          public void onSuccess() {
    
    
               EMLog.e(TAG, "onSuccess" );
               runOnUiThread(new Runnable() {
    
    
                @Override
                 public void run() {
    
    
                      Uri localUrlUri = ((EMImageMessageBody) msg.getBody()).getLocalUri();
                      Glide.with(mContext)
                             .load(localUrlUri)
                             .apply(new RequestOptions().error(default_res))
                             .into(image);
                      }
              });
           }

        public void onError(final int error, String message) {
    
    
             EMLog.e(TAG, "offline file transfer error:" + message);
         }
 
        public void onProgress(final int progress, String status) {
    
    
              EMLog.d(TAG, "Progress: " + progress);
            }
      };
       msg.setMessageStatusCallback(callback);
      EMClient.getInstance().chatManager().downloadAttachment(msg);

二、 如果对本地存储的路径有特殊要求:

1 可以先通过EMFileMessageBody#setlocalUrl去修改路径;
2 然后再调用EMChatManager#downloadAttachment下载(下载操作可以参考上边);

EMImageMessageBody imgBody = (EMImageMessageBody) message.getBody();
//本地文件的资源路径 
imgBody.setLocalUrl(localUrl);

在这里插入图片描述

场景二: 附件类文件存储在自己服务器

一、发送自定义消息时,携带文件存储的url;

EMMessage customMessage =EMMessage.createSendMessage(EMMessage.Type.CUSTOM);
  // `event` 为需要传递的自定义消息事件,比如礼物消息,可以设置:
        String event = "gift";
        EMCustomMessageBody customBody = new EMCustomMessageBody(event);
        // `params` 类型为 `Map`。
        Map params = new HashMap<>();
        params.put("imageUrl","服务器的图片url");
        customBody.setParams(params);
        customMessage.addBody(customBody);
        // `to` 指另一方环信用户 ID(或者群组 ID,聊天室 ID)
        customMessage.setTo(to);
        // 如果是群聊,设置 `ChatType` 为 `GroupChat`,该参数默认是单聊(`Chat`)。
        customMessage.setChatType(chatType);
        EMClient.getInstance().chatManager().sendMessage(customMessage);

在这里插入图片描述

二 、接收消息时,解析字段获取到url,进行下载;

在这里插入图片描述

@Override
   public void onMessageReceived(List messages) {
    
    
        super.onMessageReceived(messages);
        for (EMMessage message : messages) {
    
    
            EMCustomMessageBody emCustomMessageBody = (EMCustomMessageBody) message.getBody();
            Map params = emCustomMessageBody.getParams();
            String imageUrl = params.get("imageUrl");
        }
    }

猜你喜欢

转载自blog.csdn.net/huan132456765/article/details/131461660