use of com.easemob.chat.NormalFileMessageBody in project wechat by motianhuo.
the class MessageAdapter method handleFileMessage.
/**
* 文件消息
*
* @param message
* @param holder
* @param position
* @param convertView
*/
private void handleFileMessage(final EMMessage message, final ViewHolder holder, int position, View convertView) {
final NormalFileMessageBody fileMessageBody = (NormalFileMessageBody) message.getBody();
final String filePath = fileMessageBody.getLocalUrl();
holder.tv_file_name.setText(fileMessageBody.getFileName());
holder.tv_file_size.setText(TextFormater.getDataSize(fileMessageBody.getFileSize()));
holder.ll_container.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
File file = new File(filePath);
if (file != null && file.exists()) {
// 文件存在,直接打开
FileUtils.openFile(file, (Activity) context);
} else {
// 下载
// TODO 下载文件
context.startActivity(new Intent(context, ShowNormalFileActivity.class).putExtra("msgbody", fileMessageBody));
}
if (message.direct == EMMessage.Direct.RECEIVE && !message.isAcked) {
try {
EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
message.isAcked = true;
} catch (EaseMobException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
String st1 = context.getResources().getString(R.string.Have_downloaded);
String st2 = context.getResources().getString(R.string.Did_not_download);
if (message.direct == EMMessage.Direct.RECEIVE) {
// 接收的消息
System.err.println("it is receive msg");
File file = new File(filePath);
if (file != null && file.exists()) {
holder.tv_file_download_state.setText(st1);
} else {
holder.tv_file_download_state.setText(st2);
}
return;
}
// until here, deal with send voice msg
switch(message.status) {
case SUCCESS:
holder.pb.setVisibility(View.INVISIBLE);
holder.tv.setVisibility(View.INVISIBLE);
holder.staus_iv.setVisibility(View.INVISIBLE);
break;
case FAIL:
holder.pb.setVisibility(View.INVISIBLE);
holder.tv.setVisibility(View.INVISIBLE);
holder.staus_iv.setVisibility(View.VISIBLE);
break;
case INPROGRESS:
if (timers.containsKey(message.getMsgId()))
return;
// set a timer
final Timer timer = new Timer();
timers.put(message.getMsgId(), timer);
timer.schedule(new TimerTask() {
@Override
public void run() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
holder.pb.setVisibility(View.VISIBLE);
holder.tv.setVisibility(View.VISIBLE);
holder.tv.setText(message.progress + "%");
if (message.status == EMMessage.Status.SUCCESS) {
holder.pb.setVisibility(View.INVISIBLE);
holder.tv.setVisibility(View.INVISIBLE);
timer.cancel();
} else if (message.status == EMMessage.Status.FAIL) {
holder.pb.setVisibility(View.INVISIBLE);
holder.tv.setVisibility(View.INVISIBLE);
holder.staus_iv.setVisibility(View.VISIBLE);
Toast.makeText(activity, activity.getString(R.string.send_fail) + activity.getString(R.string.connect_failuer_toast), 0).show();
timer.cancel();
}
}
});
}
}, 0, 500);
break;
default:
// 发送消息
sendMsgInBackground(message, holder);
}
}
use of com.easemob.chat.NormalFileMessageBody in project wechat by motianhuo.
the class ChatActivity method sendFile.
/**
* 发送文件
*
* @param uri
*/
private void sendFile(Uri uri) {
String filePath = null;
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
filePath = cursor.getString(column_index);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
filePath = uri.getPath();
}
File file = new File(filePath);
if (file == null || !file.exists()) {
String st7 = getResources().getString(R.string.File_does_not_exist);
Toast.makeText(getApplicationContext(), st7, 0).show();
return;
}
if (file.length() > 10 * 1024 * 1024) {
String st6 = getResources().getString(R.string.The_file_is_not_greater_than_10_m);
Toast.makeText(getApplicationContext(), st6, 0).show();
return;
}
// 创建一个文件消息
EMMessage message = EMMessage.createSendMessage(EMMessage.Type.FILE);
// 如果是群聊,设置chattype,默认是单聊
if (chatType == CHATTYPE_GROUP)
message.setChatType(ChatType.GroupChat);
message.setReceipt(toChatUsername);
// add message body
NormalFileMessageBody body = new NormalFileMessageBody(new File(filePath));
message.addBody(body);
conversation.addMessage(message);
listView.setAdapter(adapter);
adapter.refresh();
listView.setSelection(listView.getCount() - 1);
setResult(RESULT_OK);
}
Aggregations