0

I made a chat app and in that i made a feature to send image in chat, the image should upload in firebase storage and its link should be stored in firebase database,

the image gets uploaded in firebase storage but its link is not saved in database instead this link gets saved -

"com.google.android.gms.tasks.zzu@4298b5d"

here is the code -

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1 && resultCode == RESULT_OK)
        {
            Uri url = data.getData();

            DatabaseReference messageRef = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUserId).child(otherUserId).push();
            final String messageId = messageRef.getKey();

            DatabaseReference notificationRef = FirebaseDatabase.getInstance().getReference().child("Notifications").child(otherUserId).push();
            final String notificationId = notificationRef.getKey();

            StorageReference file = FirebaseStorage.getInstance().getReference().child("message_images").child(messageId + ".jpg");

            file.putFile(url).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>()
            {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task)
                {
                    if(task.isSuccessful())
                    {
                        String imageUrl = task.getResult().getStorage().getDownloadUrl().toString();

                        Map messageMap = new HashMap();
                        messageMap.put("message", imageUrl);
                        messageMap.put("type", "image");
                        messageMap.put("from", currentUserId);
                        messageMap.put("to", otherUserId);
                        messageMap.put("timestamp", ServerValue.TIMESTAMP);

                        HashMap<String, String> notificationData = new HashMap<>();
                        notificationData.put("from", currentUserId);
                        notificationData.put("type", "message");

                        Map userMap = new HashMap();
                        userMap.put("Messages/" + currentUserId + "/" + otherUserId + "/" + messageId, messageMap);
                        userMap.put("Messages/" + otherUserId + "/" + currentUserId + "/" + messageId, messageMap);

                        userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/message", "You have sent a picture.");
                        userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/timestamp", ServerValue.TIMESTAMP);
                        userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/seen", ServerValue.TIMESTAMP);

                        userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/message", "Has send you a picture.");
                        userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/timestamp", ServerValue.TIMESTAMP);
                        userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/seen", 0);

                        userMap.put("Notifications/" + otherUserId + "/" + notificationId, notificationData);

                        FirebaseDatabase.getInstance().getReference().updateChildren(userMap, new DatabaseReference.CompletionListener()
                        {
                            @Override
                            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference)
                            {
                                sendButton.setEnabled(true);

                                if(databaseError != null)
                                {
                                    Log.d(TAG, "sendMessage(): updateChildren failed: " + databaseError.getMessage());
                                }
                            }
                        });
                    }
                }
            });
        }
    }
Ashvin Bhagat
  • 213
  • 4
  • 14
  • 1
    Check **[this](https://stackoverflow.com/questions/53299915/how-to-get-offline-uploaded-file-download-url-in-firebase/53300660#53300660)** out. – Alex Mamo Apr 28 '20 at 16:19

1 Answers1

0

I got the solution myself, this is the solution.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK) {
            Uri url = data.getData();

            DatabaseReference messageRef = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUserId).child(otherUserId).push();
            final String messageId = messageRef.getKey();

            DatabaseReference notificationRef = FirebaseDatabase.getInstance().getReference().child("Notifications").child(otherUserId).push();
            final String notificationId = notificationRef.getKey();

            final StorageReference file = FirebaseStorage.getInstance().getReference().child("message_images").child(messageId + ".jpg");

            file.putFile(url).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    file.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            String imageUrl = uri.toString();

                            Map messageMap = new HashMap();
                            messageMap.put("message", imageUrl);
                            messageMap.put("type", "image");
                            messageMap.put("from", currentUserId);
                            messageMap.put("to", otherUserId);
                            messageMap.put("timestamp", ServerValue.TIMESTAMP);

                            HashMap<String, String> notificationData = new HashMap<>();
                            notificationData.put("from", currentUserId);
                            notificationData.put("type", "message");

                            Map userMap = new HashMap();
                            userMap.put("Messages/" + currentUserId + "/" + otherUserId + "/" + messageId, messageMap);
                            userMap.put("Messages/" + otherUserId + "/" + currentUserId + "/" + messageId, messageMap);

                            userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/message", "You have sent a picture.");
                            userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/timestamp", ServerValue.TIMESTAMP);
                            userMap.put("Chat/" + currentUserId + "/" + otherUserId + "/seen", ServerValue.TIMESTAMP);

                            userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/message", "Has send you a picture.");
                            userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/timestamp", ServerValue.TIMESTAMP);
                            userMap.put("Chat/" + otherUserId + "/" + currentUserId + "/seen", 0);

                            userMap.put("Notifications/" + otherUserId + "/" + notificationId, notificationData);

                            FirebaseDatabase.getInstance().getReference().updateChildren(userMap, new DatabaseReference.CompletionListener() {
                                @Override
                                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                                    sendButton.setEnabled(true);

                                    if (databaseError != null) {
                                        Log.d(TAG, "sendMessage(): updateChildren failed: " + databaseError.getMessage());
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }
    }
Ashvin Bhagat
  • 213
  • 4
  • 14