注册

EMClient.getInstance().chatManager().importMessages怎么插入图片或视频信息呢?

请问下EMClient.getInstance().chatManager().importMessages怎么插入图片或视频信息呢?插入文字的格式好像不行,谢谢了,纠结几天了!
已邀请:
请参考
public static void importMessages() {
List<EMMessage> messageList = new ArrayList<EMMessage>();
try {
JSONArray array = new JSONArray(getJson("ceshi.json"));
Log.d("json", array.toString());
// for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(0);
long timestamp = jsonObject.optLong("timestamp");
String from = jsonObject.optString("from");
String to = jsonObject.optString("to");
String msgId = jsonObject.optString("msg_id");
String chatType = jsonObject.optString("chat_type");

JSONObject bodyObject = jsonObject.optJSONObject("payload").optJSONArray("bodies").getJSONObject(0);
String type = bodyObject.optString("type");
EMMessage message = null;
if (type.equals("txt")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
EMTextMessageBody body = new EMTextMessageBody(bodyObject.optString("msg"));
message.addBody(body);
} else if (type.equals("img")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.IMAGE);
File file = new File("");
// 这里使用反射获取 ImageBody,为了设置 size
Class<?> bodyClass = Class.forName("com.hyphenate.chat.EMImageMessageBody");
Class<?>[] parTypes = new Class<?>[1];
parTypes[0] = File.class;
Constructor<?> constructor = bodyClass.getDeclaredConstructor(parTypes);
Object[] pars = new Object[1];
pars[0] = file;
EMImageMessageBody body = (EMImageMessageBody) constructor.newInstance(pars);
Method setSize = Class.forName("com.hyphenate.chat.EMImageMessageBody")
.getDeclaredMethod("setSize", int.class, int.class);
setSize.setAccessible(true);
int width = bodyObject.optJSONObject("size").optInt("width");
int height = bodyObject.optJSONObject("size").optInt("height");
setSize.invoke(body, width, height);

body.setFileName(bodyObject.optString("filename"));
body.setSecret(bodyObject.optString("secret"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setThumbnailUrl(bodyObject.optString("url"));
// to make it compatible with thumbnail received in previous version
String thumbPath = getThumbnailImagePath(bodyObject.optString("url"));
body.setThumbnailLocalPath(thumbPath);
String localPath = getLocalPath(bodyObject.optString("url"));
body.setLocalUrl(localPath);
Log.d("thumbPath", "send"+thumbPath);
message.addBody(body);
}
else if (type.equals("video")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.VIDEO);
EMVideoMessageBody body = new EMVideoMessageBody();
body.setThumbnailUrl(bodyObject.optString("thumb"));
body.setThumbnailSecret(bodyObject.optString("thumb_secret"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setVideoFileLength(bodyObject.optLong("file_length"));
body.setSecret(bodyObject.optString("secret"));
message.addBody(body);
} else if (type.equals("audio")) {
message = EMMessage.createReceiveMessage(EMMessage.Type.VOICE);
File file = new File("");
EMVoiceMessageBody body = new EMVoiceMessageBody(file, bodyObject.optInt("length"));
body.setRemoteUrl(bodyObject.optString("url"));
body.setSecret(bodyObject.optString("secret"));
body.setFileName(bodyObject.optString("filename"));
message.addBody(body);
}

message.setFrom(from);
message.setTo(to);
message.setMsgTime(timestamp);
message.setLocalTime(timestamp);
message.setMsgId(msgId);
if(chatType.equals("chat")){
message.setChatType(EMMessage.ChatType.Chat);
} else if(chatType.equals("GroupChat")){
message.setChatType(EMMessage.ChatType.GroupChat);
} else if(chatType.equals("ChatRoom")){
message.setChatType(EMMessage.ChatType.ChatRoom);
}
message.setStatus(EMMessage.Status.SUCCESS);
// messageList.add(message);
// }
// Log.d("conversation1", EMClient.getInstance().chatManager().getAllConversations().size()+"");
EMClient.getInstance().chatManager().saveMessage(message);

要回复问题请先登录注册