Search in sources :

Example 1 with ImageMessageBody

use of com.easemob.chat.ImageMessageBody in project wechat by motianhuo.

the class MessageAdapter method showImageView.

/**
	 * load image into image view
	 * 
	 * @param thumbernailPath
	 * @param iv
	 * @param position
	 * @return the image exists or not
	 */
private boolean showImageView(final String thumbernailPath, final ImageView iv, final String localFullSizePath, String remoteDir, final EMMessage message) {
    // String imagename =
    // localFullSizePath.substring(localFullSizePath.lastIndexOf("/") + 1,
    // localFullSizePath.length());
    // final String remote = remoteDir != null ? remoteDir+imagename :
    // imagename;
    final String remote = remoteDir;
    EMLog.d("###", "local = " + localFullSizePath + " remote: " + remote);
    // first check if the thumbnail image already loaded into cache
    Bitmap bitmap = ImageCache.getInstance().get(thumbernailPath);
    if (bitmap != null) {
        // thumbnail image is already loaded, reuse the drawable
        iv.setImageBitmap(bitmap);
        iv.setClickable(true);
        iv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO 查看大图
                System.err.println("image view on click");
                Intent intent = new Intent(activity, ShowBigImage.class);
                File file = new File(localFullSizePath);
                if (file.exists()) {
                    Uri uri = Uri.fromFile(file);
                    intent.putExtra("uri", uri);
                    System.err.println("here need to check why download everytime");
                } else {
                    // The local full size pic does not exist yet.
                    // ShowBigImage needs to download it from the server
                    // first
                    // intent.putExtra("", message.get);
                    ImageMessageBody body = (ImageMessageBody) message.getBody();
                    intent.putExtra("secret", body.getSecret());
                    intent.putExtra("remotepath", remote);
                }
                if (message != null && message.direct == EMMessage.Direct.RECEIVE && !message.isAcked && message.getChatType() != ChatType.GroupChat) {
                    try {
                        EMChatManager.getInstance().ackMessageRead(message.getFrom(), message.getMsgId());
                        message.isAcked = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                activity.startActivity(intent);
            }
        });
        return true;
    } else {
        new LoadImageTask().execute(thumbernailPath, localFullSizePath, remote, message.getChatType(), iv, activity, message);
        return true;
    }
}
Also used : Bitmap(android.graphics.Bitmap) LoadImageTask(com.juns.wechat.chat.task.LoadImageTask) ShowBigImage(com.juns.wechat.chat.ShowBigImage) OnClickListener(android.view.View.OnClickListener) Intent(android.content.Intent) ImageMessageBody(com.easemob.chat.ImageMessageBody) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) File(java.io.File) Uri(android.net.Uri) EaseMobException(com.easemob.exceptions.EaseMobException)

Example 2 with ImageMessageBody

use of com.easemob.chat.ImageMessageBody in project wechat by motianhuo.

the class ChatActivity method forwardMessage.

/**
	 * 转发消息
	 * 
	 * @param forward_msg_id
	 */
protected void forwardMessage(String forward_msg_id) {
    EMMessage forward_msg = EMChatManager.getInstance().getMessage(forward_msg_id);
    EMMessage.Type type = forward_msg.getType();
    switch(type) {
        case TXT:
            // 获取消息内容,发送消息
            String content = ((TextMessageBody) forward_msg.getBody()).getMessage();
            sendText(content);
            break;
        case IMAGE:
            // 发送图片
            String filePath = ((ImageMessageBody) forward_msg.getBody()).getLocalUrl();
            if (filePath != null) {
                File file = new File(filePath);
                if (!file.exists()) {
                    // 不存在大图发送缩略图
                    filePath = ImageUtils.getThumbnailImagePath(filePath);
                }
                sendPicture(filePath);
            }
            break;
        default:
            break;
    }
}
Also used : TextMessageBody(com.easemob.chat.TextMessageBody) ImageMessageBody(com.easemob.chat.ImageMessageBody) File(java.io.File) EMMessage(com.easemob.chat.EMMessage)

Example 3 with ImageMessageBody

use of com.easemob.chat.ImageMessageBody in project wechat by motianhuo.

the class ChatActivity method sendPicture.

/**
	 * 发送图片
	 * 
	 * @param filePath
	 */
private void sendPicture(final String filePath) {
    String to = toChatUsername;
    // create and add image message in view
    final EMMessage message = EMMessage.createSendMessage(EMMessage.Type.IMAGE);
    // 如果是群聊,设置chattype,默认是单聊
    if (chatType == CHATTYPE_GROUP)
        message.setChatType(ChatType.GroupChat);
    message.setReceipt(to);
    ImageMessageBody body = new ImageMessageBody(new File(filePath));
    // 默认超过100k的图片会压缩后发给对方,可以设置成发送原图
    // body.setSendOriginalImage(true);
    message.addBody(body);
    conversation.addMessage(message);
    listView.setAdapter(adapter);
    adapter.refresh();
    listView.setSelection(listView.getCount() - 1);
    setResult(RESULT_OK);
// more(more);
}
Also used : ImageMessageBody(com.easemob.chat.ImageMessageBody) File(java.io.File) EMMessage(com.easemob.chat.EMMessage)

Example 4 with ImageMessageBody

use of com.easemob.chat.ImageMessageBody in project wechat by motianhuo.

the class NewMsgAdpter method getMessageDigest.

/**
	 * 根据消息内容和消息类型获取消息内容提示
	 * 
	 * @param message
	 * @param context
	 * @return
	 */
private String getMessageDigest(EMMessage message, Context context) {
    String digest = "";
    switch(message.getType()) {
        case // 位置消息
        LOCATION:
            if (message.direct == EMMessage.Direct.RECEIVE) {
                digest = getStrng(context, R.string.location_recv);
                String name = message.getFrom();
                if (GloableParams.UserInfos != null) {
                    User user = GloableParams.Users.get(message.getFrom());
                    if (user != null && null != user.getUserName())
                        name = user.getUserName();
                }
                digest = String.format(digest, name);
                return digest;
            } else {
                digest = getStrng(context, R.string.location_prefix);
            }
            break;
        case // 图片消息
        IMAGE:
            ImageMessageBody imageBody = (ImageMessageBody) message.getBody();
            digest = getStrng(context, R.string.picture) + imageBody.getFileName();
            break;
        case // 语音消息
        VOICE:
            digest = getStrng(context, R.string.voice_msg);
            break;
        case // 视频消息
        VIDEO:
            digest = getStrng(context, R.string.video);
            break;
        case // 文本消息
        TXT:
            if (!message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)) {
                TextMessageBody txtBody = (TextMessageBody) message.getBody();
                digest = txtBody.getMessage();
            } else {
                TextMessageBody txtBody = (TextMessageBody) message.getBody();
                digest = getStrng(context, R.string.voice_call) + txtBody.getMessage();
            }
            break;
        case // 普通文件消息
        FILE:
            digest = getStrng(context, R.string.file);
            break;
        default:
            System.err.println("error, unknow type");
            return "";
    }
    return digest;
}
Also used : User(com.juns.wechat.bean.User) ImageMessageBody(com.easemob.chat.ImageMessageBody) TextMessageBody(com.easemob.chat.TextMessageBody)

Example 5 with ImageMessageBody

use of com.easemob.chat.ImageMessageBody in project wechat by motianhuo.

the class MessageAdapter method handleImageMessage.

/**
	 * 图片消息
	 * 
	 * @param message
	 * @param holder
	 * @param position
	 * @param convertView
	 */
private void handleImageMessage(final EMMessage message, final ViewHolder holder, final int position, View convertView) {
    holder.pb.setTag(position);
    holder.iv.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            activity.startActivityForResult((new Intent(activity, ContextMenu.class)).putExtra("position", position).putExtra("type", EMMessage.Type.IMAGE.ordinal()), ChatActivity.REQUEST_CODE_CONTEXT_MENU);
            return true;
        }
    });
    // 接收方向的消息
    if (message.direct == EMMessage.Direct.RECEIVE) {
        // "it is receive msg";
        if (message.status == EMMessage.Status.INPROGRESS) {
            // "!!!! back receive";
            holder.iv.setImageResource(R.drawable.default_image);
            showDownloadImageProgress(message, holder);
        // downloadImage(message, holder);
        } else {
            // "!!!! not back receive, show image directly");
            holder.pb.setVisibility(View.GONE);
            holder.tv.setVisibility(View.GONE);
            holder.iv.setImageResource(R.drawable.default_image);
            ImageMessageBody imgBody = (ImageMessageBody) message.getBody();
            if (imgBody.getLocalUrl() != null) {
                // String filePath = imgBody.getLocalUrl();
                String remotePath = imgBody.getRemoteUrl();
                String filePath = ImageUtils.getImagePath(remotePath);
                String thumbRemoteUrl = imgBody.getThumbnailUrl();
                String thumbnailPath = ImageUtils.getThumbnailImagePath(thumbRemoteUrl);
                showImageView(thumbnailPath, holder.iv, filePath, imgBody.getRemoteUrl(), message);
            }
        }
        return;
    }
    // 发送的消息
    // process send message
    // send pic, show the pic directly
    ImageMessageBody imgBody = (ImageMessageBody) message.getBody();
    String filePath = imgBody.getLocalUrl();
    if (filePath != null && new File(filePath).exists()) {
        showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, null, message);
    } else {
        showImageView(ImageUtils.getThumbnailImagePath(filePath), holder.iv, filePath, IMAGE_DIR, message);
    }
    switch(message.status) {
        case SUCCESS:
            holder.pb.setVisibility(View.GONE);
            holder.tv.setVisibility(View.GONE);
            holder.staus_iv.setVisibility(View.GONE);
            break;
        case FAIL:
            holder.pb.setVisibility(View.GONE);
            holder.tv.setVisibility(View.GONE);
            holder.staus_iv.setVisibility(View.VISIBLE);
            break;
        case INPROGRESS:
            holder.staus_iv.setVisibility(View.GONE);
            holder.pb.setVisibility(View.VISIBLE);
            holder.tv.setVisibility(View.VISIBLE);
            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() {

                        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.GONE);
                                holder.tv.setVisibility(View.GONE);
                                // message.setSendingStatus(Message.SENDING_STATUS_SUCCESS);
                                timer.cancel();
                            } else if (message.status == EMMessage.Status.FAIL) {
                                holder.pb.setVisibility(View.GONE);
                                holder.tv.setVisibility(View.GONE);
                                // message.setSendingStatus(Message.SENDING_STATUS_FAIL);
                                // message.setProgress(0);
                                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:
            sendPictureMessage(message, holder);
    }
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) OnLongClickListener(android.view.View.OnLongClickListener) Intent(android.content.Intent) ContextMenu(com.juns.wechat.chat.ContextMenu) ImageMessageBody(com.easemob.chat.ImageMessageBody) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) File(java.io.File)

Aggregations

ImageMessageBody (com.easemob.chat.ImageMessageBody)5 File (java.io.File)4 Intent (android.content.Intent)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 EMMessage (com.easemob.chat.EMMessage)2 TextMessageBody (com.easemob.chat.TextMessageBody)2 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 OnClickListener (android.view.View.OnClickListener)1 OnLongClickListener (android.view.View.OnLongClickListener)1 EaseMobException (com.easemob.exceptions.EaseMobException)1 User (com.juns.wechat.bean.User)1 ContextMenu (com.juns.wechat.chat.ContextMenu)1 ShowBigImage (com.juns.wechat.chat.ShowBigImage)1 LoadImageTask (com.juns.wechat.chat.task.LoadImageTask)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1