0

Please check the code i am getting an error at getDownloadUrl() method. As it is not being accepted by the firebase. I am a beginner at this, so can anyone please suggest the changes i have to make in this code.

This snippet is basically focused on getting the image url from the storage and add it in the database section in firebase. But the getDownloadUrl is not working, please suggest the changes in the snippet to make it happen.

            StorageReference filePath = userProfileImagesReference.child(currentUserID + ".jpg");

            filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if(task.isSuccessful())
                    {
                        Toast.makeText(SettingsActivity.this, "Profile Image Uploaded", Toast.LENGTH_SHORT).show();

                        //error here
                        final String downloadURL = task.getResult().getDownloadUrl().toString();

                        rootRef.child("Users").child(currentUserID).child("image")
                                .setValue(downloadURL)
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if(task.isSuccessful())
                                        {
                                            Toast.makeText(SettingsActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }
                                        else
                                        {
                                            String errorMSG = task.getException().toString();
                                            Toast.makeText(SettingsActivity.this, "Error: "+errorMSG, Toast.LENGTH_SHORT).show();
                                        loadingBar.dismiss();
                                        }
                                    }
                                });


                    }
                    else
                    {
                        String error = task.getException().toString();
                        Toast.makeText(SettingsActivity.this, "Error: "+error, Toast.LENGTH_SHORT).show();
                        loadingBar.dismiss();
                    }
                }
            });
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    You're not using the API correctly. getDownloadUrl returns a Task that gives you the URL asynchronously. I suggest reviewing the documentation. https://firebase.google.com/docs/storage/android/download-files#download_data_via_url – Doug Stevenson Jul 13 '20 at 15:34
  • Could you please add the exception you are getting. – Ekta Dushanj Jul 13 '20 at 16:52

1 Answers1

0

I guess you need to get it directly from storageReference , so do as following :

Uri downloadUrl = filePath.getDownloadUrl();

Taki
  • 3,290
  • 1
  • 16
  • 41