私聊改造:新增快速发送图片功能

This commit is contained in:
huangjian
2023-01-12 18:34:48 +08:00
parent 69f0d76330
commit dee4f873b4
18 changed files with 494 additions and 261 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@@ -3,6 +3,6 @@
<item android:drawable="@drawable/nim_message_input_emotion_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/nim_message_input_emotion_pressed" android:state_focused="true"/>
<item android:drawable="@drawable/nim_message_input_emotion"/>
<item android:drawable="@drawable/nim_message_input_emotion_pressed"/>
</selector>

View File

@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/color_grey_eaeaea" />
<corners android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"/>
<corners android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomRightRadius="8dp"
android:bottomLeftRadius="8dp"/>
</shape>

View File

@@ -28,7 +28,7 @@ public class PhotoAction extends PickImageAction {
}
@Override
protected void onPicked(File file) {
public void onPicked(File file) {
IMMessage message;
if (getContainer() != null && getContainer().sessionType == SessionTypeEnum.ChatRoom) {
message = ChatRoomMessageBuilder.createChatRoomImageMessage(getAccount(), file, file.getName());

View File

@@ -54,7 +54,6 @@ public class EmoticonPickerView extends LinearLayout implements IEmoticonCategor
}
};
private int categoryIndex;
private Handler uiHandler;
public EmoticonPickerView(Context context) {
super(context);
@@ -75,7 +74,6 @@ public class EmoticonPickerView extends LinearLayout implements IEmoticonCategor
private void init(Context context) {
this.context = context;
this.uiHandler = new Handler(context.getMainLooper());
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.nim_emoji_layout, this);
}
@@ -222,7 +220,7 @@ public class EmoticonPickerView extends LinearLayout implements IEmoticonCategor
@Override
public void run() {
if (scrollView.getChildAt(0).getWidth() == 0) {
uiHandler.postDelayed(this, 100);
scrollView.postDelayed(this, 100);
}
int x = -1;
View child = tabView.getChildAt(index);
@@ -236,7 +234,7 @@ public class EmoticonPickerView extends LinearLayout implements IEmoticonCategor
}
}
};
uiHandler.postDelayed(runnable, 100);
scrollView.postDelayed(runnable, 100);
}

View File

@@ -8,6 +8,7 @@ import android.text.TextUtils;
import android.widget.Toast;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.business.session.actions.PhotoAction;
import com.netease.nim.uikit.business.session.constant.Extras;
import com.netease.nim.uikit.common.media.picker.model.PhotoInfo;
import com.netease.nim.uikit.common.media.picker.model.PickerContract;
@@ -24,10 +25,6 @@ import java.util.ArrayList;
import java.util.List;
public class SendImageHelper {
public interface Callback {
void sendImage(File file, boolean isOrig);
}
public static void sendImageAfterPreviewPhotoActivityResult(Intent data, Callback callback) {
final ArrayList<String> selectedImageFileList = data.getStringArrayListExtra(Extras.EXTRA_SCALED_IMAGE_LIST);
final ArrayList<String> origSelectedImageFileList = data.getStringArrayListExtra(Extras.EXTRA_ORIG_IMAGE_LIST);
@@ -88,6 +85,16 @@ public class SendImageHelper {
}
}
public static void sendPhotos(Context context, PhotoAction action, List<PhotoInfo> photos, boolean isOrig) {
for (PhotoInfo photoInfo : photos) {
new SendImageTask(context, isOrig, photoInfo, (file, isOrig1) -> action.onPicked(file)).execute();
}
}
public interface Callback {
void sendImage(File file, boolean isOrig);
}
// 从相册选择图片进行发送(Added by NYB)
public static class SendImageTask extends AsyncTask<Void, Void, File> {

View File

@@ -128,4 +128,12 @@ public abstract class MsgViewHolderThumbBase extends MsgViewHolderBase {
}
protected abstract String thumbFromSourceFile(String path);
protected int leftBackground() {
return 0;
}
protected int rightBackground() {
return 0;
}
}

View File

@@ -2,10 +2,18 @@ package com.netease.nim.uikit.common.media.picker;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.business.session.module.input.NimImageActionEvent;
import com.netease.nim.uikit.common.media.dao.MediaDAO;
import com.netease.nim.uikit.common.media.picker.activity.PickImageActivity;
import com.netease.nim.uikit.common.media.picker.model.AlbumInfo;
import com.netease.nim.uikit.common.media.picker.model.PhotoInfo;
import com.netease.nim.uikit.common.media.picker.util.ThumbnailsUtil;
import com.netease.nim.uikit.common.ui.dialog.CustomAlertDialog;
import com.netease.nim.uikit.common.util.storage.StorageType;
import com.netease.nim.uikit.common.util.storage.StorageUtil;
@@ -13,11 +21,18 @@ import com.netease.nim.uikit.common.util.string.StringUtil;
import org.greenrobot.eventbus.EventBus;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by huangjun on 2015/9/22.
*/
public class PickImageHelper {
public static final String FILE_PREFIX = "file://";
/**
* 打开图片选择器
*/
@@ -72,6 +87,112 @@ public class PickImageHelper {
EventBus.getDefault().post(event);
}
public static ArrayList<AlbumInfo> getAllMediaPhotos(Context context, int limit) {
getAllMediaThumbnails(context);
ArrayList<AlbumInfo> albumInfolist = new ArrayList<>();
int num = 0;
Cursor cursorPhotos = null;
try {
cursorPhotos = MediaDAO.getAllMediaPhotos(context);
HashMap<String, AlbumInfo> hash = new HashMap<>();
AlbumInfo albumInfo;
PhotoInfo photoInfo;
if (cursorPhotos != null && cursorPhotos.moveToFirst()) {
do {
int index = 0;
int _id = cursorPhotos.getInt(cursorPhotos.getColumnIndex(MediaStore.Images.Media._ID));
String path = cursorPhotos.getString(cursorPhotos.getColumnIndex(MediaStore.Images.Media.DATA));
String album = cursorPhotos.getString(cursorPhotos.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
long size = cursorPhotos.getLong(cursorPhotos.getColumnIndex(MediaStore.Images.Media.SIZE));
if (!isValidImageFile(path)) {
Log.d("PICKER", "it is not a vaild path:" + path);
continue;
}
num++;
List<PhotoInfo> photoList = new ArrayList<>();
photoInfo = new PhotoInfo();
if (hash.containsKey(album)) {
albumInfo = hash.remove(album);
if (albumInfolist.contains(albumInfo))
index = albumInfolist.indexOf(albumInfo);
photoInfo.setImageId(_id);
photoInfo.setFilePath(FILE_PREFIX + path);
photoInfo.setAbsolutePath(path);
photoInfo.setSize(size);
albumInfo.getList().add(photoInfo);
albumInfolist.set(index, albumInfo);
hash.put(album, albumInfo);
} else {
albumInfo = new AlbumInfo();
photoList.clear();
photoInfo.setImageId(_id);
photoInfo.setFilePath(FILE_PREFIX + path);
photoInfo.setAbsolutePath(path);
photoInfo.setSize(size);
photoList.add(photoInfo);
albumInfo.setImageId(_id);
albumInfo.setFilePath(FILE_PREFIX + path);
albumInfo.setAbsolutePath(path);
albumInfo.setAlbumName(album);
albumInfo.setList(photoList);
albumInfolist.add(albumInfo);
hash.put(album, albumInfo);
}
} while (cursorPhotos.moveToNext() && num < limit);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (cursorPhotos != null) {
cursorPhotos.close();
}
} catch (Exception e) {
}
}
return albumInfolist;
}
private static void getAllMediaThumbnails(Context context) {
ThumbnailsUtil.clear();
Cursor cursorThumb = null;
try {
cursorThumb = MediaDAO.getAllMediaThumbnails(context);
if (cursorThumb != null && cursorThumb.moveToFirst()) {
int imageID;
String imagePath;
do {
imageID = cursorThumb.getInt(cursorThumb.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID));
imagePath = cursorThumb.getString(cursorThumb.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
ThumbnailsUtil.put(imageID, FILE_PREFIX + imagePath);
} while (cursorThumb.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (cursorThumb != null) {
cursorThumb.close();
}
} catch (Exception e) {
}
}
}
private static boolean isValidImageFile(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return false;
}
File imageFile = new File(filePath);
return imageFile.exists();
}
public static class PickImageOption {
/**
* 图片选择器标题

View File

@@ -1,13 +1,8 @@
package com.netease.nim.uikit.common.media.picker.fragment;
import android.app.Activity;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Thumbnails;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -19,33 +14,21 @@ import android.widget.TextView;
import com.netease.nim.uikit.R;
import com.netease.nim.uikit.common.fragment.TFragment;
import com.netease.nim.uikit.common.media.dao.MediaDAO;
import com.netease.nim.uikit.common.media.picker.PickImageHelper;
import com.netease.nim.uikit.common.media.picker.adapter.PickerAlbumAdapter;
import com.netease.nim.uikit.common.media.picker.model.AlbumInfo;
import com.netease.nim.uikit.common.media.picker.model.PhotoInfo;
import com.netease.nim.uikit.common.media.picker.util.ThumbnailsUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class PickerAlbumFragment extends TFragment implements OnItemClickListener {
public interface OnAlbumItemClickListener {
public void OnAlbumItemClick(AlbumInfo info);
}
private OnAlbumItemClickListener onAlbumItemClickListener;
private LinearLayout loadingLay;
private TextView loadingTips;
private TextView loadingEmpty;
private ListView albumListView;
public static final String FILE_PREFIX = "file://";
private List<AlbumInfo> albumInfolist = new ArrayList<AlbumInfo>();
private List<AlbumInfo> albumInfolist = new ArrayList<>();
private PickerAlbumAdapter albumAdapter;
public PickerAlbumFragment() {
@@ -90,13 +73,31 @@ public class PickerAlbumFragment extends TFragment implements OnItemClickListene
new ImageScanAsyncTask().execute();
}
private void getAllMediaPhotos() {
if (albumInfolist == null) {
albumInfolist = new ArrayList<>();
} else {
albumInfolist.clear();
}
albumInfolist.addAll(PickImageHelper.getAllMediaPhotos(getActivity(),Integer.MAX_VALUE));
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
onAlbumItemClickListener.OnAlbumItemClick(albumInfolist.get(position));
}
public interface OnAlbumItemClickListener {
void OnAlbumItemClick(AlbumInfo info);
}
private class ImageScanAsyncTask extends AsyncTask<Void, Void, Object> {
@Override
protected Object doInBackground(Void... params) {
getAllMediaThumbnails();
getAllMediaPhotos();
return null;
}
@@ -117,118 +118,4 @@ public class PickerAlbumFragment extends TFragment implements OnItemClickListene
}
}
}
private void getAllMediaThumbnails() {
ThumbnailsUtil.clear();
Cursor cursorThumb = null;
try {
cursorThumb = MediaDAO.getAllMediaThumbnails(getActivity());
if (cursorThumb != null && cursorThumb.moveToFirst()) {
int imageID;
String imagePath;
do {
imageID = cursorThumb.getInt(cursorThumb.getColumnIndex(Thumbnails.IMAGE_ID));
imagePath = cursorThumb.getString(cursorThumb.getColumnIndex(Thumbnails.DATA));
ThumbnailsUtil.put(imageID, FILE_PREFIX + imagePath);
} while (cursorThumb.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (cursorThumb != null) {
cursorThumb.close();
}
} catch (Exception e) {
}
}
}
private void getAllMediaPhotos() {
if (albumInfolist == null) {
albumInfolist = new ArrayList<AlbumInfo>();
} else {
albumInfolist.clear();
}
Cursor cursorPhotos = null;
try {
cursorPhotos = MediaDAO.getAllMediaPhotos(getActivity());
HashMap<String, AlbumInfo> hash = new HashMap<String, AlbumInfo>();
AlbumInfo albumInfo = null;
PhotoInfo photoInfo = null;
if (cursorPhotos != null && cursorPhotos.moveToFirst()) {
do {
int index = 0;
int _id = cursorPhotos.getInt(cursorPhotos.getColumnIndex(MediaStore.Images.Media._ID));
String path = cursorPhotos.getString(cursorPhotos.getColumnIndex(MediaStore.Images.Media.DATA));
String album = cursorPhotos.getString(cursorPhotos.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
long size = cursorPhotos.getLong(cursorPhotos.getColumnIndex(MediaStore.Images.Media.SIZE));
if (!isValidImageFile(path)) {
Log.d("PICKER", "it is not a vaild path:" + path);
continue;
}
List<PhotoInfo> photoList = new ArrayList<PhotoInfo>();
photoInfo = new PhotoInfo();
if (hash.containsKey(album)) {
albumInfo = hash.remove(album);
if (albumInfolist.contains(albumInfo))
index = albumInfolist.indexOf(albumInfo);
photoInfo.setImageId(_id);
photoInfo.setFilePath(FILE_PREFIX + path);
photoInfo.setAbsolutePath(path);
photoInfo.setSize(size);
albumInfo.getList().add(photoInfo);
albumInfolist.set(index, albumInfo);
hash.put(album, albumInfo);
} else {
albumInfo = new AlbumInfo();
photoList.clear();
photoInfo.setImageId(_id);
photoInfo.setFilePath(FILE_PREFIX + path);
photoInfo.setAbsolutePath(path);
photoInfo.setSize(size);
photoList.add(photoInfo);
albumInfo.setImageId(_id);
albumInfo.setFilePath(FILE_PREFIX + path);
albumInfo.setAbsolutePath(path);
albumInfo.setAlbumName(album);
albumInfo.setList(photoList);
albumInfolist.add(albumInfo);
hash.put(album, albumInfo);
}
} while (cursorPhotos.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (cursorPhotos != null) {
cursorPhotos.close();
}
} catch (Exception e) {
}
}
}
private boolean isValidImageFile(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return false;
}
File imageFile = new File(filePath);
if (imageFile.exists()) {
return true;
}
return false;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
onAlbumItemClickListener.OnAlbumItemClick(albumInfolist.get(position));
}
}

View File

@@ -11,6 +11,7 @@ public class PhotoInfo implements Serializable {
private String absolutePath;
private long size;
private boolean choose = false;
private int index;
public int getImageId() {
return imageId;
@@ -51,4 +52,24 @@ public class PhotoInfo implements Serializable {
public void setSize(long size) {
this.size = size;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString() {
return "PhotoInfo{" +
"imageId=" + imageId +
", filePath='" + filePath + '\'' +
", absolutePath='" + absolutePath + '\'' +
", size=" + size +
", choose=" + choose +
", index=" + index +
'}';
}
}