附件类文件存储在环信和不存储在环信时的两种实现方式
场景一: 附件类文件存储在环信服务器
EMImageMessageBody imgBody = (EMImageMessageBody) message.getBody();
//本地文件的资源路径
String imageLocalUrl = imgBody.getLocalUrl();
Glide.with(this).load(imageLocalUrl).into(image);
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);
EMImageMessageBody imgBody = (EMImageMessageBody) message.getBody();
//本地文件的资源路径
String imageLocalUrl = imgBody.getLocalUrl();
Glide.with(this).load(imageLocalUrl).into(image);
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);
二、 如果对本地存储的路径有特殊要求:
EMImageMessageBody imgBody = (EMImageMessageBody) message.getBody();
//本地文件的资源路径
imgBody.setLocalUrl(localUrl);
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);
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");
}
}
@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");
}
}