레이아웃 코드는 제외한다.



Module 수준의 Gradle 파일에 라이브러리를 설치하자!
dependencies {
// ...
implementation("com.google.firebase:firebase-storage")
}
StorageReference storageRef;storageRef = FirebaseStorage.getInstance().getReference(chatKey).child("images");// ImageView에 이미지를 적용하기 전에 적용되어 있는 File을 쪼개서 파이어베이스 저장소에 채팅방의 고유 키 값의 경로로 저장시킨다.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageData = baos.toByteArray();
UploadTask uploadTask = storageRef.child("/" + photoFile.getName()).putBytes(imageData);

uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// 올린게 성공하면, 메시지에 해당 사진의 경로를 담아서 보내자.
Message message = new Message(mAuth.getCurrentUser().getUid(),
chatKey + "/images/" + photoFile.getName(), getDateTimeString(), true);
myRef.child(chatKey).child("messages").push().setValue(message);
}
});
