package com.fanxin.easeui.widget.chatrow; import java.io.File; import com.fanxin.easeui.utils.EaseCommonUtils; import com.fanxin.easeui.utils.EaseImageUtils; import com.hyphenate.chat.EMClient; import com.hyphenate.chat.EMFileMessageBody; import com.hyphenate.chat.EMImageMessageBody; import com.hyphenate.chat.EMMessage; import com.hyphenate.chat.EMMessage.ChatType; import com.hyphenate.easeui.R; import com.fanxin.easeui.model.EaseImageCache; import com.fanxin.easeui.ui.EaseShowBigImageActivity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.AsyncTask; import android.view.View; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class EaseChatRowImage extends EaseChatRowFile{ protected ImageView imageView; private EMImageMessageBody imgBody; public EaseChatRowImage(Context context, EMMessage message, int position, BaseAdapter adapter) { super(context, message, position, adapter); } @Override protected void onInflatView() { inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ? R.layout.ease_row_received_picture : R.layout.ease_row_sent_picture, this); } @Override protected void onFindViewById() { percentageView = (TextView) findViewById(R.id.percentage); imageView = (ImageView) findViewById(R.id.image); } @Override protected void onSetUpView() { imgBody = (EMImageMessageBody) message.getBody(); // received messages if (message.direct() == EMMessage.Direct.RECEIVE) { if (imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.DOWNLOADING || imgBody.thumbnailDownloadStatus() == EMFileMessageBody.EMDownloadStatus.PENDING) { imageView.setImageResource(R.drawable.ease_default_image); setMessageReceiveCallback(); } else { progressBar.setVisibility(View.GONE); percentageView.setVisibility(View.GONE); imageView.setImageResource(R.drawable.ease_default_image); String thumbPath = imgBody.thumbnailLocalPath(); if (!new File(thumbPath).exists()) { // to make it compatible with thumbnail received in previous version thumbPath = EaseImageUtils.getThumbnailImagePath(imgBody.getLocalUrl()); } showImageView(thumbPath, imageView, imgBody.getLocalUrl(), message); } return; } String filePath = imgBody.getLocalUrl(); String thumbPath = EaseImageUtils.getThumbnailImagePath(imgBody.getLocalUrl()); showImageView(thumbPath, imageView, filePath, message); handleSendMessage(); } @Override protected void onUpdateView() { super.onUpdateView(); } @Override protected void onBubbleClick() { Intent intent = new Intent(context, EaseShowBigImageActivity.class); File file = new File(imgBody.getLocalUrl()); if (file.exists()) { Uri uri = Uri.fromFile(file); intent.putExtra("uri", uri); } else { // The local full size pic does not exist yet. // ShowBigImage needs to download it from the server // first intent.putExtra("secret", imgBody.getSecret()); intent.putExtra("remotepath", imgBody.getRemoteUrl()); intent.putExtra("localUrl", imgBody.getLocalUrl()); } if (message != null && message.direct() == EMMessage.Direct.RECEIVE && !message.isAcked() && message.getChatType() == ChatType.Chat) { try { EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId()); } catch (Exception e) { e.printStackTrace(); } } context.startActivity(intent); } /** * load image into image view * * @param thumbernailPath * @param iv * @param * @return the image exists or not */ private boolean showImageView(final String thumbernailPath, final ImageView iv, final String localFullSizePath,final EMMessage message) { // first check if the thumbnail image already loaded into cache Bitmap bitmap = EaseImageCache.getInstance().get(thumbernailPath); if (bitmap != null) { // thumbnail image is already loaded, reuse the drawable iv.setImageBitmap(bitmap); return true; } else { new AsyncTask<Object, Void, Bitmap>() { @Override protected Bitmap doInBackground(Object... args) { File file = new File(thumbernailPath); if (file.exists()) { return EaseImageUtils.decodeScaleImage(thumbernailPath, 420, 420); } else if (new File(imgBody.thumbnailLocalPath()).exists()) { return EaseImageUtils.decodeScaleImage(imgBody.thumbnailLocalPath(), 420, 420); } else { if (message.direct() == EMMessage.Direct.SEND) { if (localFullSizePath != null && new File(localFullSizePath).exists()) { return EaseImageUtils.decodeScaleImage(localFullSizePath, 420, 420); } else { return null; } } else { return null; } } } protected void onPostExecute(Bitmap image) { if (image != null) { iv.setImageBitmap(image); EaseImageCache.getInstance().put(thumbernailPath, image); } else { if (message.status() == EMMessage.Status.FAIL) { if (EaseCommonUtils.isNetWorkConnected(activity)) { new Thread(new Runnable() { @Override public void run() { EMClient.getInstance().chatManager().downloadThumbnail(message); } }).start(); } } } } }.execute(); return true; } } }