现在我正在为Android创建一个聊天应用程序,我已经做了所有事情,当我创建聊天活动时,我创建了消息传递功能并且它工作正常,但是当我添加发送图像功能时,我有一个恼人的问题。
当发送的消息和图片图片开始消失,短信混合在我的Android聊天应用程序中时。
我已经看过两个不同的教学课程来创建聊天应用程序,但我遇到了同样的问题。
我尝试使用 Glide 和 Picasso 来显示图片,但我遇到了同样的问题。
消息适配器
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private List<Messages> userMessList;
private FirebaseAuth mAuth;
private DatabaseReference uDR;
public MessageAdapter(List<Messages> userMessList)
{
this.userMessList = userMessList;
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.mess_sing, parent, false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(v);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onBindViewHolder(@NonNull final MessageViewHolder holder, int position) {
String mCurrUid = mAuth.getCurrentUser().getUid();
Messages messages = userMessList.get(position);
String from_uid = messages.getFrom();
String mess_type = messages.getType();
uDR = FirebaseDatabase.getInstance().getReference().child("Users").child(from_uid);
uDR.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String uImg = dataSnapshot.child("th_image").getValue().toString();
Picasso.with(holder.mess_pic.getContext()).load(uImg).into(holder.mess_pic);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
FrameLayout.LayoutParams paramsF, paramsS;
paramsF = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);
paramsS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT);
paramsF.gravity = Gravity.RIGHT;
paramsS.gravity = Gravity.LEFT;
paramsS.leftMargin = 120;
if (mess_type.equals("text"))
{
holder.mess_img.setVisibility(View.GONE);
if (Objects.equals(from_uid ,mCurrUid))
{
holder.mess_text.setLayoutParams(paramsF);
holder.mess_pic.setVisibility(View.GONE);
holder.mess_text.setBackgroundResource(R.drawable.mess_fir_sty);
holder.mess_text.setTextColor(Color.parseColor("#494949"));
}
else
{
holder.mess_text.setLayoutParams(paramsS);
holder.mess_pic.setVisibility(View.VISIBLE);
holder.mess_text.setBackgroundResource(R.drawable.mess_sec_sty);
holder.mess_text.setTextColor(Color.WHITE);
}
holder.mess_text.setText(messages.getMessage());
}
else
{
holder.mess_text.setVisibility(View.GONE);
if (Objects.equals(from_uid ,mCurrUid))
{
holder.mess_pic.setVisibility(View.GONE);
Picasso.with(holder.mess_pic.getContext()).load(messages.getMessage()).into(holder.mess_img);
}
else
{
holder.mess_pic.setVisibility(View.VISIBLE);
Picasso.with(holder.mess_pic.getContext()).load(messages.getMessage()).into(holder.mess_img);
}
}
}
@Override
public int getItemCount() {
return userMessList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder
{
public TextView mess_text;
public CircleImageView mess_pic;
public ImageView mess_img;
public MessageViewHolder (View view)
{
super(view);
mess_text = (TextView) view.findViewById(R.id.mess_text);
mess_pic = (CircleImageView) view.findViewById(R.id.mess_profile_pic);
mess_img = (ImageView) view.findViewById(R.id.mess_img);
}
}
}
您可能需要的重要代码部分聊天活动
public class ChatActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
messageAdapter = new MessageAdapter(messagesList);
linearLayoutManager = new LinearLayoutManager(this);
mess_uList.setHasFixedSize(true);
mess_uList.setLayoutManager(linearLayoutManager);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
linearLayoutManager.setSmoothScrollbarEnabled(true);
mess_uList.setAdapter(messageAdapter);
FetchMessages();
mRootRef.child("Users").child(uId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String online = dataSnapshot.child("online").getValue().toString();
if(online.equals("true")) {
u_c_last.setText("Online");
} else {
GetTimeAgo getTimeAgo = new GetTimeAgo();
long lastTime = Long.parseLong(online);
String lastSeenTime = getTimeAgo.getTimeAgo(lastTime, getApplicationContext());
u_c_last.setText(lastSeenTime);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
chat_send_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendMessage();
}
});
c_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
input_chat_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MessToBottom();
}
});
chat_add_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent gallery_pic = new Intent ();
gallery_pic.setType("image/*");
gallery_pic.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(gallery_pic, "Select Picture"),Gallery_Pick);
}
});
}
private void FetchMessages()
{
mRootRef.child("Messages").child(mCurrentUserId).child(uId).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Messages messages = dataSnapshot.getValue(Messages.class);
messagesList.add(messages);
messageAdapter.notifyDataSetChanged();
MessToBottom();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void SendMessage()
{
final String input_text = input_chat_message.getText().toString();
if (input_text.matches(""))
{
Toast.makeText(getApplicationContext(),"Write your message to send!",Toast.LENGTH_SHORT).show();
}
else {
String mess_seing_ref = "Messages/" + mCurrentUserId + "/" + uId;
String mess_reing_ref = "Messages/" + uId + "/" + mCurrentUserId;
DatabaseReference u_mess_key = mRootRef.child("Messages").child(mCurrentUserId).child(uId).push();
String mess_push_id = u_mess_key.getKey();
Map mess_text_body = new HashMap();
mess_text_body.put("message", input_text);
mess_text_body.put("seen", false);
mess_text_body.put("type", "text");
mess_text_body.put("time", ServerValue.TIMESTAMP);
mess_text_body.put("from",mCurrentUserId);
Map mess_body_det = new HashMap();
mess_body_det.put(mess_seing_ref + "/" + mess_push_id, mess_text_body);
mess_body_det.put(mess_reing_ref + "/" + mess_push_id, mess_text_body);
mp.start();
mRootRef.updateChildren(mess_body_det, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null)
{
Log.d("Send_Log", databaseError.getMessage().toString());
}
input_chat_message.setText("");
MessToBottom();
}
});
}
}
public void MessToBottom()
{
mess_uList.scrollToPosition(messageAdapter.getItemCount()-1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Gallery_Pick && resultCode == RESULT_OK && data != null) {
Uri imageUri = data.getData();
final String mess_seing_ref = "Messages/" + mCurrentUserId + "/" + uId;
final String mess_reing_ref = "Messages/" + uId + "/" + mCurrentUserId;
DatabaseReference u_mess_key = mRootRef.child("Messages").child(mCurrentUserId).child(uId).push();
final String mess_push_id = u_mess_key.getKey();
StorageReference file_path = picStorge.child(mess_push_id + ".jpg");
file_path.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful())
{
final String down_url = task.getResult().getDownloadUrl().toString();
Map mess_text_body = new HashMap();
mess_text_body.put("message", down_url);
mess_text_body.put("seen", false);
mess_text_body.put("type", "image");
mess_text_body.put("time", ServerValue.TIMESTAMP);
mess_text_body.put("from",mCurrentUserId);
Map mess_body_det = new HashMap();
mess_body_det.put(mess_seing_ref + "/" + mess_push_id, mess_text_body);
mess_body_det.put(mess_reing_ref + "/" + mess_push_id, mess_text_body);
mRootRef.updateChildren(mess_body_det, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null)
{
Log.d("Send_Log", databaseError.getMessage().toString());
}
input_chat_message.setText("");
MessToBottom();
}
});
Toast.makeText(getApplicationContext(),"Sent!",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Failed, Check your internet, Try agian later.", Toast.LENGTH_LONG).show();
}
}
});
}
}
}
任何人都可以决定我的问题的原因或需要更多的代码吗? 我因为这个问题而停止了,我无法完成我的工作。