利用文件存储实现存储聊天记录中的图片
首先先需要使聊天界面可以发送图片,然后再对其进行存储操作
①修改Msg类,增加了imgpath,即图片路径
public class Msg {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SENT = 1;
public static final int TYPE_RECEIVED_IMG = 2;
public static final int TYPE_SENT_IMG = 3;
private String content;
private String imgpath;
private int type;
public Msg(String content,String imgpath, int type){
this.content = content;
this.imgpath = imgpath;
this.type = type;
}
public String getContent(){
return content;
}
public int getType(){
return type;
}
public String getImgpath() {
return imgpath;
}
}
②对MsgAdapter进行了大部分修改,添加了图片有关部分,以及图片的存储到本地,以及将消息的各部分分开处理,原先只有收发两类,现修改成了switch语句判断,有文字消息的收发和图片消息的收发,同时也更加便于之后的拓展
public class MsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<Msg> mMsgList;
static class R_MsgViewHolder extends RecyclerView.ViewHolder{
LinearLayout rightLayout;
TextView rightMsg;
public R_MsgViewHolder(View view){
super(view);
rightLayout = (LinearLayout)view.findViewById(R.id.right_layout);
rightMsg = (TextView)view.findViewById(R.id.right_msg);
}
}
static class L_MsgViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftLayout;
TextView leftMsg;
public L_MsgViewHolder(View view){
super(view);
leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
leftMsg = (TextView)view.findViewById(R.id.left_msg);
}
}
static class R_ImgViewHolder extends RecyclerView.ViewHolder{
LinearLayout rightlayout_img;
ImageView rightImage;
public R_ImgViewHolder(View view){
super(view);
rightlayout_img = (LinearLayout)view.findViewById(R.id.right_layout_img);
rightImage = (ImageView)view.findViewById(R.id.right_image);
}
}
static class L_ImgViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftlayout_img;
ImageView leftImage;
public L_ImgViewHolder(View view){
super(view);
leftlayout_img = (LinearLayout)view.findViewById(R.id.left_layout_img);
leftImage = (ImageView)view.findViewById(R.id.left_image);
}
}
public MsgAdapter(List<Msg> msgList){
mMsgList = msgList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHold