imjstx
imjstx
  • 发布:2022-03-27 12:47
  • 更新:2022-10-20 15:36
  • 阅读:451

即时通讯聊天系统源码下载带教程

分类:HTML5+

本文将讨论实现一套即时通讯聊天系统源码并附部署教程,该im源码具有以下功能:
带app源码:im.jstxym.top
我们将创建一个聊天布局,并在聊天发送消息。
●用户可以发送消息或图像。
●用户可以使用相机或图库发送图像。
●首先,请求许可将被要求发送图像使用图库或点击图像后使用相机。
●如果允许,那么用户可以发送图像,或者它将再次请求请求许可。
分步实施
步骤1:创建两个新的布局资源文件,命名为row_chat_left和row_chat_right
使用row_chat_left.xml文件。收到的信息将在左侧。类似地,使用row_chat_right.xml文件。发送给用户的消息将在右边。下面是row_chat_left.xml文件和row_chat_right.xml文件的代码。
<LinearLayout
android:id="@+id/msglayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profilec"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/profile_image"
app:civ_border_color="@null"/>
<TextView
android:id="@+id/msgc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/bg_receiver"
android:padding="15dp"
android:text="HisMessage"
android:textColor="@color/colorBlack"
android:textSize="16sp"
android:visibility="gone"/>
<ImageView
android:id="@+id/images"
android:layout_width="200dp"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:background="@drawable/bg_receiver"
android:padding="15dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_images"/>
<TextView
android:id="@+id/timetv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="02/01/199006:19PM"
android:textColor="@color/colorBlack"
android:textSize="12sp"/>
</LinearLayout>
<TextView
android:id="@+id/isSeen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:text="Delivered"
android:textAlignment="textEnd"
android:visibility="gone"/>
</LinearLayout>
步骤2:使用activity_chat.xml文件
在RecyclerView中,我们将显示所有的消息。在TextView中,用户将键入消息,并使用发送按钮,用户将发送消息。下面是activity_chat.xml文件的代码。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChatActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profiletv"
android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="centerCrop"
android:src="@drawable/profile_image"
app:civ_circle_background_color="@color/colorPrimaryDark"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/nameptv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="HisName"
android:textColor="@color/colorWhite"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/onlinetv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Online"
android:textColor="@color/colorWhite"
android:textStyle="bold"/>
</LinearLayout>
<ImageView
android:id="@+id/block"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:src="@drawable/ic_unblock"/>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/chatrecycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/chatlayout"
android:layout_below="@id/toolbar"/>
<LinearLayout
android:id="@+id/chatlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorWhite"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/attachbtn"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@null"
android:src="@drawable/ic_iattach"/>
<EditText
android:id="@+id/messaget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:hint="StartTyping"
android:inputType="textCapSentences|textMultiLine"
android:padding="15dp"/>
<ImageButton
android:id="@+id/sendmsg"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@null"
android:src="@drawable/send_message"/>
</LinearLayout>
</RelativeLayout>
步骤3:使用row_chatlist.xml文件
创建另一个布局资源文件,命名为row_chatlist。下面是row_chatlist.xml文件的代码。
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:contentPadding="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profileimage"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/profile_image"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/onlinestatus"
android:layout_width="25dp"
android:layout_height="25dp"/>
<TextView
android:id="@+id/nameonline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_toEndOf="@id/profileimage"
android:layout_toRightOf="@id/profileimage"
android:text="HisName"
android:textColor="@color/colorBlack"
android:textSize="18sp"/>
<TextView
android:id="@+id/lastmessge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nameonline"
android:layout_marginStart="4dp"
android:layout_toEndOf="@id/profileimage"
android:layout_toRightOf="@id/profileimage"
android:maxLines="2"
android:text="LastMessage"
android:textColor="@color/colorBlack"/>
<ImageView
android:id="@+id/blocking"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_unblock"/>
<ImageView
android:id="@+id/seen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/blocking"
android:layout_alignParentEnd="true"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_unblock"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
步骤4:使用ModelChat.java文件
创建这个类来初始化键,以便我们以后可以检索键的值。
packagecom.example.socialmediaapp;
publicclassModelChat{
Stringmessage;
publicStringgetMessage(){
returnmessage;
}
publicvoidsetMessage(Stringmessage){
this.message=message;
}
publicStringgetReceiver(){
returnreceiver;
}
publicvoidsetReceiver(Stringreceiver){
this.receiver=receiver;
}
publicStringgetSender(){
returnsender;
}
publicvoidsetSender(Stringsender){
this.sender=sender;
}
publicStringgetTimestamp(){
returntimestamp;
}
publicvoidsetTimestamp(Stringtimestamp){
this.timestamp=timestamp;
}
publicbooleanisDilihat(){
returndilihat;
}
publicvoidsetDilihat(booleandilihat){
this.dilihat=dilihat;
}
Stringreceiver;
publicModelChat(){
}
Stringsender;
publicStringgetType(){
returntype;
}
publicvoidsetType(Stringtype){
this.type=type;
}
publicModelChat(Stringmessage,Stringreceiver,Stringsender,Stringtype,Stringtimestamp,booleandilihat){
this.message=message;
this.receiver=receiver;
this.sender=sender;
this.type=type;
this.timestamp=timestamp;
this.dilihat=dilihat;
}
Stringtype;
Stringtimestamp;
booleandilihat;
}
步骤5:使用AdpaterChat.java文件
创建一个新的java类,并将该类命名为AdpaterChat。下面是AdpaterChat.java文件的代码。
packagecom.example.socialmediaapp;
importandroid.app.AlertDialog;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.text.format.DateFormat;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
importandroid.widget.Toast;
importandroidx.annotation.NonNull;
importandroidx.recyclerview.widget.RecyclerView;
importcom.bumptech.glide.Glide;
importcom.google.firebase.auth.FirebaseAuth;
importcom.google.firebase.auth.FirebaseUser;
importcom.google.firebase.database.DataSnapshot;
importcom.google.firebase.database.DatabaseError;
importcom.google.firebase.database.DatabaseReference;
importcom.google.firebase.database.FirebaseDatabase;
importcom.google.firebase.database.Query;
importcom.google.firebase.database.ValueEventListener;
importjava.util.Calendar;
importjava.util.List;
importjava.util.Locale;
importde.hdodenhof.circleimageview.CircleImageView;
publicclassAdapterChatextendsRecyclerView.Adapter<com.example.socialmediaapp.AdapterChat.Myholder>{
privatestaticfinalintMSG_TYPE_LEFT=0;
privatestaticfinalintMSG_TYPR_RIGHT=1;
Contextcontext;
List<ModelChat>list;
Stringimageurl;
FirebaseUserfirebaseUser;
publicAdapterChat(Contextcontext,List<ModelChat>list,Stringimageurl){
this.context=context;
this.list=list;
this.imageurl=imageurl;
}
@NonNull
@Override
publicMyholderonCreateViewHolder(@NonNullViewGroupparent,intviewType){
if(viewType==MSG_TYPE_LEFT){
Viewview=LayoutInflater.from(context)。inflate(R.layout.row_chat_left,parent,false);
returnnewMyholder(view);
}else{
Viewview=LayoutInflater.from(context)。inflate(R.layout.row_chat_right,parent,false);
returnnewMyholder(view);
}
}
@Override
publicvoidonBindViewHolder(@NonNullMyholderholder,finalintposition){
Stringmessage=list.get(position)。getMessage();
StringtimeStamp=list.get(position)。getTimestamp();
Stringtype=list.get(position)。getType();
Calendarcalendar=Calendar.getInstance(Locale.ENGLISH);
calendar.setTimeInMillis(Long.parseLong(timeStamp));
Stringtimedate=DateFormat.format("dd/MM/yyyyhh:mmaa",calendar)。toString();
holder.message.setText(message);
holder.time.setText(timedate);
try{
Glide.with(context)。load(imageurl)。into(holder.image);
}catch(Exceptione){
}
if(type.equals("text")){
holder.message.setVisibility(View.VISIBLE);
holder.mimage.setVisibility(View.GONE);
holder.message.setText(message);
}else{
holder.message.setVisibility(View.GONE);
holder.mimage.setVisibility(View.VISIBLE);
Glide.with(context)。load(message)。into(holder.mimage);
}
holder.msglayput.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
builder.setTitle("DeleteMessage");
builder.setMessage("AreYouSureToDeleteThisMessage");
builder.setPositiveButton("Delete",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
deleteMsg(position);
}
});
builder.setNegativeButton("Cancel",newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
dialog.dismiss();
}
});
builder.create()。show();
}
});
}
privatevoiddeleteMsg(intposition){
finalStringmyuid=FirebaseAuth.getInstance()。getCurrentUser()。getUid();
Stringmsgtimestmp=list.get(position)。getTimestamp();
DatabaseReferencedbref=FirebaseDatabase.getInstance()。getReference()。child("Chats");
Queryquery=dbref.orderByChild("timestamp")。equalTo(msgtimestmp);
query.addListenerForSingleValueEvent(newValueEventListener(){
@Override
publicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){
for(DataSnapshotdataSnapshot1:dataSnapshot.getChildren()){
if(dataSnapshot1.child("sender")。getValue()。equals(myuid)){
//anytwoofbelowcanbeused
dataSnapshot1.getRef()。removeValue();
/HashMap<String,Object>hashMap=newHashMap<>();
hashMap.put("message","ThisMessageWasDeleted");
dataSnapshot1.getRef()。updateChildren(hashMap);
Toast.makeText(context,"MessageDeleted……",Toast.LENGTH_LONG)。show();
/
}else{
Toast.makeText(context,"youcandeletonlyyourmsg…",Toast.LENGTH_LONG)。show();
}
}
}
@Override
publicvoidonCancelled(@NonNullDatabaseErrordatabaseError){
}
});
}
@Override
publicintgetItemCount(){
returnlist.size();
}
@Override
publicintgetItemViewType(intposition){
firebaseUser=FirebaseAuth.getInstance()。getCurrentUser();
if(list.get(position)。getSender()。equals(firebaseUser.getUid())){
returnMSG_TYPR_RIGHT;
}else{
returnMSG_TYPE_LEFT;
}
}
classMyholderextendsRecyclerView.ViewHolder{
CircleImageViewimage;
ImageViewmimage;
TextViewmessage,time,isSee;
LinearLayoutmsglayput;
publicMyholder(@NonNullViewitemView){
super(itemView);
image=itemView.findViewById(R.id.profilec);
message=itemView.findViewById(R.id.msgc);
time=itemView.findViewById(R.id.timetv);
isSee=itemView.findViewById(R.id.isSeen);
msglayput=itemView.findViewById(R.id.msglayout);
mimage=itemView.findViewById(R.id.images);
}
}
}
步骤6:使用ChatActivity.java文件
我们正在阅读来自Firebase中的“聊天”节点的用户消息。每次数据更改时,该数据也会相应更改
chatList=newArrayList<>();
DatabaseReferencedbref=FirebaseDatabase.getInstance()。getReference()。child("Chats");
使用适配器聊天加载数据设置数据值
ModelChatmodelChat=dataSnapshot1.getValue(ModelChat.class);
if(modelChat.getSender()。equals(myuid)&&
modelChat.getReceiver()。equals(uid)||
modelChat.getReceiver()。equals(myuid)
&&modelChat.getSender()。equals(uid)){
chatList.add(modelChat);//addthechatinchatlist
}
adapterChat=newAdapterChat(ChatActivity.this,chatList,image);
adapterChat.notifyDataSetChanged();
recyclerView.setAdapter(adapterChat);
在聊天参考节点值中发送消息。下面是我们如何在Firebase实时数据库中保存数据
DatabaseReferencedatabaseReference=FirebaseDatabase.getInstance()。getReference();
Stringtimestamp=String.valueOf(System.currentTimeMillis());
HashMap<String,Object>hashMap=newHashMap<>();
hashMap.put("sender",myuid);
hashMap.put("receiver",uid);
hashMap.put("message",message);
hashMap.put("timestamp",timestamp);
hashMap.put("dilihat",false);
hashMap.put("type","text");
databaseReference.child("Chats")。push()。setValue(hashMap);
下面是ChatActivity.java文件的代码。
packagecom.example.socialmediaapp;
importandroid.Manifest;
importandroid.app.AlertDialog;
importandroid.app.ProgressDialog;
importandroid.content.ContentValues;
importandroid.content.DialogInterface;
importandroid.content.Intent;
importandroid.content.pm.PackageManager;
importandroid.graphics.Bitmap;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.provider.MediaStore;
importandroid.text.TextUtils;
importandroid.text.format.DateFormat;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.widget.EditText;
importandroid.widget.ImageButton;
importandroid.widget.ImageView;
importandroid.widget.TextView;
importandroid.widget.Toast;
importandroidx.annotation.NonNull;
importandroidx.annotation.Nullable;
importandroidx.appcompat.app.AppCompatActivity;
importandroidx.appcompat.widget.Toolbar;
importandroidx.core.content.ContextCompat;
importandroidx.recyclerview.widget.LinearLayoutManager;
importandroidx.recyclerview.widget.RecyclerView;
importcom.bumptech.glide.Glide;
importcom.google.android.gms.tasks.OnFailureListener;
importcom.google.android.gms.tasks.OnSuccessListener;
importcom.google.android.gms.tasks.Task;
importcom.google.firebase.auth.FirebaseAuth;
importcom.google.firebase.auth.FirebaseUser;
importcom.google.firebase.database.DataSnapshot;
importcom.google.firebase.database.DatabaseError;
importcom.google.firebase.database.DatabaseReference;
importcom.google.firebase.database.FirebaseDatabase;
importcom.google.firebase.database.Query;
importcom.google.firebase.database.ValueEventListener;
importcom.google.firebase.storage.FirebaseStorage;
importcom.google.firebase.storage.StorageReference;
importcom.google.firebase.storage.UploadTask;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Calendar;
importjava.util.HashMap;
importjava.util.List;
publicclassChatActivityextendsAppCompatActivity{
Toolbartoolbar;
RecyclerViewrecyclerView;
ImageViewprofile,block;
TextViewname,userstatus;
EditTextmsg;
ImageButtonsend,attach;
FirebaseAuthfirebaseAuth;
Stringuid,myuid,image;
ValueEventListenervalueEventListener;
List<ModelChat>chatList;
AdapterChatadapterChat;
privatestaticfinalintIMAGEPICK_GALLERY_REQUEST=300;
privatestaticfinalintIMAGE_PICKCAMERA_REQUEST=400;
privatestaticfinalintCAMERA_REQUEST=100;
privatestaticfinalintSTORAGE_REQUEST=200;
StringcameraPermission[];
StringstoragePermission[];
Uriimageuri=null;
FirebaseDatabasefirebaseDatabase;
DatabaseReferenceusers;
booleannotify=false;
booleanisBlocked=false;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
firebaseAuth=FirebaseAuth.getInstance();
//initialisethetextviewsandlayouts
profile=findViewById(R.id.profiletv);
name=findViewById(R.id.nameptv);
userstatus=findViewById(R.id.onlinetv);
msg=findViewById(R.id.messaget);
send=findViewById(R.id.sendmsg);
attach=findViewById(R.id.attachbtn);
block=findViewById(R.id.block);
LinearLayoutManagerlinearLayoutManager=newLinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
recyclerView=findViewById(R.id.chatrecycle);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
uid=getIntent()。getStringExtra("uid");
//gettinguidofanotheruserusingintent
firebaseDatabase=FirebaseDatabase.getInstance();
//initialisingpermissions
cameraPermission=newString[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE};
storagePermission=newString[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
checkUserStatus();
users=firebaseDatabase.getReference("Users");
attach.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
showImagePicDialog();
}
});
send.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
notify=true;
Stringmessage=msg.getText()。toString()。trim();
if(TextUtils.isEmpty(message)){//ifempty
Toast.makeText(ChatActivity.this,"PleaseWriteSomethingHere",Toast.LENGTH_LONG)。show();
}else{
sendmessage(message);
}
msg.setText("");
}
});
Queryuserquery=users.orderByChild("uid")。equalTo(uid);
userquery.addValueEventListener(newValueEventListener(){
@Override
publicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){
//retrieveuserdata
for(DataSnapshotdataSnapshot1:dataSnapshot.getChildren()){
Stringnameh=""+dataSnapshot1.child("name")。getValue();
image=""+dataSnapshot1.child("image")。getValue();
Stringonlinestatus=""+dataSnapshot1.child("onlineStatus")。getValue();
Stringtypingto=""+dataSnapshot1.child("typingTo")。getValue();
if(typingto.equals(myuid)){//ifuseristypingtomychat
userstatus.setText("Typing…");//typestatusastyping
}else{
if(onlinestatus.equals("online")){
userstatus.setText(onlinestatus);
}else{
Calendarcalendar=Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(onlinestatus));
Stringtimedate=DateFormat.format("dd/MM/yyyyhh:mmaa",calendar)。toString();
userstatus.setText("LastSeen:"+timedate);
}
}
name.setText(nameh);
try{
Glide.with(ChatActivity.this)。load(image)。placeholder(R.drawable.profile_image)。into(profile);
}catch(Exceptione){
}
}
}
@Override
publicvoidonCancelled(@NonNullDatabaseErrordatabaseError){
}
});
readMessages();
}
@Override
protectedvoidonPause(){
super.onPause();
Stringtimestamp=String.valueOf(System.currentTimeMillis());
checkOnlineStatus(timestamp);
checkTypingStatus("noOne");
}
@Override
protectedvoidonResume(){
checkOnlineStatus("online");
super.onResume();
}
@Override
publicbooleanonSupportNavigateUp(){
onBackPressed();
returnsuper.onSupportNavigateUp();
}
privatevoidcheckOnlineStatus(Stringstatus){
//checkonlinestatus
DatabaseReferencedbref=FirebaseDatabase.getInstance()。getReference("Users")。child(myuid);
HashMap<String,Object>hashMap=newHashMap<>();
hashMap.put("onlineStatus",status);
dbref.updateChildren(hashMap);
}
privatevoidcheckTypingStatus(Stringtyping){
DatabaseReferencedbref=FirebaseDatabase.getInstance()。getReference("Users")。child(myuid);
HashMap<String,Object>hashMap=newHashMap<>();
hashMap.put("typingTo",typing);
dbref.updateChildren(hashMap);
}
@Override
protectedvoidonStart(){
checkUserStatus();
checkOnlineStatus("online");
super.onStart();
}
privatevoidreadMessages(){
//showmessageafterretrievingdata
chatList=newArrayList<>();
DatabaseReferencedbref=FirebaseDatabase.getInstance()。getReference()。child("Chats");
dbref.addValueEventListener(newValueEventListener(){
@Override
publicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){
chatList.clear();
for(DataSnapshotdataSnapshot1:dataSnapshot.getChildren()){
ModelChatmodelChat=dataSnapshot1.getValue(ModelChat.class);
if(modelChat.getSender()。equals(myuid)&&
modelChat.getReceiver()。equals(uid)||
modelChat.getReceiver()。equals(myuid)
&&modelChat.getSender()。equals(uid)){
chatList.add(modelChat);//addthechatinchatlist
}
adapterChat=newAdapterChat(ChatActivity.this,chatList,image);
adapterChat.notifyDataSetChanged();
recyclerView.setAdapter(adapterChat);
}
}
@Override
publicvoidonCancelled(@NonNullDatabaseErrordatabaseError){
}
});
}
privatevoidshowImagePicDialog(){
Stringoptions[]={"Camera","Gallery"};
AlertDialog.Builderbuilder=newAlertDialog.Builder(ChatActivity.this);
builder.setTitle("PickImageFrom");
builder.setItems(options,newDialogInterface.OnClickListener(){
@Override
publicvoidonClick(DialogInterfacedialog,intwhich){
if(which==0){
if(!checkCameraPermission()){//ifpermissionisnotgiven
requestCameraPermission();//requestforpermission
}else{
pickFromCamera();//ifalreadyaccessgrantedthenclick
}
}elseif(which==1){
if(!checkStoragePermission()){//ifpermissionisnotgiven
requestStoragePermission();//requestforpermission
}else{
pickFromGallery();//ifalreadyaccessgrantedthenpick
}
}
}
});
builder.create()。show();
}
publicvoidonRequestPermissionsResult(intrequestCode,@NonNullString[]permissions,@NonNullint[]grantResults){
//requestforpermissionifnotgiven
switch(requestCode){
caseCAMERA_REQUEST:{
if(grantResults.length>0){
booleancamera_accepted=grantResults[0]==PackageManager.PERMISSION_GRANTED;
booleanwriteStorageaccepted=grantResults[1]==PackageManager.PERMISSION_GRANTED;
if(camera_accepted&&writeStorageaccepted){
pickFromCamera();//ifaccessgrantedthenclick
}else{
Toast.makeText(this,"PleaseEnableCameraandStoragePermissions",Toast.LENGTH_LONG)。show();
}
}
}
break;
caseSTORAGE_REQUEST:{
if(grantResults.length>0){
booleanwriteStorageaccepted=grantResults[0]==PackageManager.PERMISSION_GRANTED;
if(writeStorageaccepted){
pickFromGallery();//ifaccessgrantedthenpick
}else{
Toast.makeText(this,"PleaseEnableStoragePermissions",Toast.LENGTH_LONG)。show();
}
}
}
break;
}
}
@Override
publicvoidonActivityResult(intrequestCode,intresultCode,@NullableIntentdata){
if(resultCode==RESULT_OK){
if(requestCode==IMAGEPICK_GALLERY_REQUEST){
imageuri=data.getData();//getimagedatatoupload
try{
sendImageMessage(imageuri);
}catch(IOExceptione){
e.printStackTrace();
}
}
if(requestCode==IMAGE_PICKCAMERA_REQUEST){
try{
sendImageMessage(imageuri);
}catch(IOExceptione){
e.printStackTrace();
}
}
}
super.onActivityResult(requestCode,resultCode,data);
}
privatevoidsendImageMessage(Uriimageuri)throwsIOException{
notify=true;
finalProgressDialogdialog=newProgressDialog(this);
dialog.setMessage("SendingImage");
dialog.show();
//Ifwearesendingimageasamessage
//thenweneedtofindtheurlof
//imageafteruploadingthe
//imageinfirebasestorage
finalStringtimestamp=""+System.currentTimeMillis();
Stringfilepathandname="ChatImages/"+"post"+timestamp;//filename
Bitmapbitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageuri);
ByteArrayOutputStreamarrayOutputStream=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,arrayOutputStream);//compressingtheimageusingbitmap
finalbyte[]data=arrayOutputStream.toByteArray();
StorageReferenceref=FirebaseStorage.getInstance()。getReference()。child(filepathandname);
ref.putBytes(data)。addOnSuccessListener(newOnSuccessListener<UploadTask.TaskSnapshot>(){
@Override
publicvoidonSuccess(UploadTask.TaskSnapshottaskSnapshot){
dialog.dismiss();
Task<Uri>uriTask=taskSnapshot.getStorage()。getDownloadUrl();
while(!uriTask.isSuccessful());
StringdownloadUri=uriTask.getResult()。toString();//gettingurliftaskissuccessful
if(uriTask.isSuccessful()){
DatabaseReferencere=FirebaseDatabase.getInstance()。getReference();
HashMap<String,Object>hashMap=newHashMap<>();
hashMap.put("sender",myuid);
hashMap.put("receiver",uid);
hashMap.put("message",downloadUri);
hashMap.put("timestamp",timestamp);
hashMap.put("dilihat",false);
hashMap.put("type","images");
re.child("Chats")。push()。setValue(hashMap);//pushinfirebaseusinguniqueid
finalDatabaseReferenceref1=FirebaseDatabase.getInstance()。getReference("ChatList")。child(uid)。child(myuid);
ref1.addValueEventListener(newValueEventListener(){
@Override
publicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){
if(!dataSnapshot.exists()){
ref1.child("id")。setValue(myuid);
}
}
@Override
publicvoidonCancelled(@NonNullDatabaseErrordatabaseError){
}
});
finalDatabaseReferenceref2=FirebaseDatabase.getInstance()。getReference("ChatList")。child(myuid)。child(uid);
ref2.addValueEventListener(newValueEventListener(){
@Override
publicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){
if(!dataSnapshot.exists()){
ref2.child("id")。setValue(uid);
}
}
@Override
publicvoidonCancelled(@NonNullDatabaseErrordatabaseError){
}
});
}
}
})。addOnFailureListener(newOnFailureListener(){
@Override
publicvoidonFailure(@NonNullExceptione){
}
});
}
privateBooleancheckCameraPermission(){
booleanresult=ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)==(PackageManager.PERMISSION_GRANTED);
booleanresult1=ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==(PackageManager.PERMISSION_GRANTED);
returnresult&&result1;
}
privatevoidrequestCameraPermission(){
requestPermissions(cameraPermission,CAMERA_REQUEST);
}
privatevoidpickFromCamera(){
ContentValuescontentValues=newContentValues();
contentValues.put(MediaStore.Images.Media.TITLE,"Temp_pic");
contentValues.put(MediaStore.Images.Media.DESCRIPTION,"TempDescription");
imageuri=this.getContentResolver()。insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
IntentcamerIntent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
camerIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageuri);
startActivityForResult(camerIntent,IMAGE_PICKCAMERA_REQUEST);
}
privatevoidpickFromGallery(){
IntentgalleryIntent=newIntent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,IMAGEPICK_GALLERY_REQUEST);
}
privateBooleancheckStoragePermission(){
booleanresult=ContextCompat.checkSelfPermission(ChatActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)==(PackageManager.PERMISSION_GRANTED);
returnresult;
}
privatevoidrequestStoragePermission(){
requestPermissions(storagePermission,STORAGE_REQUEST);
}
privatevoidsendmessage(finalStringmessage){
//creatingareferencetostoredatainfirebase
//Wewillbestoringdatausingcurrenttimein"Chatlist"
//andwearepushingdatausinguniqueidin"Chats"
DatabaseReferencedatabaseReference=FirebaseDatabase.getInstance()。getReference();
Stringtimestamp=String.valueOf(System.currentTimeMillis());
HashMap<String,Object>hashMap=newHashMap<>();
hashMap.put("sender",myuid);
hashMap.put("receiver",uid);
hashMap.put("message",message);
hashMap.put("timestamp",timestamp);
hashMap.put("dilihat",false);
hashMap.put("type","text");
databaseReference.child("Chats")。push()。setValue(hashMap);
finalDatabaseReferenceref1=FirebaseDatabase.getInstance()。getReference("ChatList")。child(uid)。child(myuid);
ref1.addValueEventListener(newValueEventListener(){
@Override
publicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){
if(!dataSnapshot.exists()){
ref1.child("id")。setValue(myuid);
}
}
@Override
publicvoidonCancelled(@NonNullDatabaseErrordatabaseError){
}
});
finalDatabaseReferenceref2=FirebaseDatabase.getInstance()。getReference("ChatList")。child(myuid)。child(uid);
ref2.addValueEventListener(newValueEventListener(){
@Override
publicvoidonDataChange(@NonNullDataSnapshotdataSnapshot){
if(!dataSnapshot.exists()){
ref2.child("id")。setValue(uid);
}
}
@Override
publicvoidonCancelled(@NonNullDatabaseErrordatabaseError){
}
});
}
@Override
publicbooleanonOptionsItemSelected(@NonNullMenuItemitem){
if(item.getItemId()==R.id.logout){
firebaseAuth.signOut();
checkUserStatus();
}
returnsuper.onOptionsItemSelected(item);
}
privatevoidcheckUserStatus(){
FirebaseUseruser=firebaseAuth.getCurrentUser();
if(user!=null){
myuid=user.getUid();
}
}
}

0 关注 分享

要回复文章请先登录注册

即时通讯开发

即时通讯开发

基于原始WebRTC音视频功能,不依赖任何三方音视频服务及原生插件 https://ext.dcloud.net.cn/plugin?id=9024
2022-10-20 15:36