0

I am trying to save the URL of an image by using the method below:

StorageReference filePath = imageStorage.child("profile_images").child(currentUserID + ".jpg");
filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
        if(task.isSuccessful())
        {
            String downloadURL = task.getResult().getStorage().getDownloadUrl().toString();
            userDataBase.child("image").setValue(downloadURL).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    progressDialog.dismiss();
                }
            });


        }
        else
        {
            progressDialog.dismiss();
            Toast.makeText(SettingsActivity.this, "Error Uploading",Toast.LENGTH_SHORT).show();
        }
    }
});
}
    String downloadURL = task.getResult().getStorage().getPath();
            userDataBase.child("image").setValue(downloadURL).addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    progressDialog.dismiss();
                }
            });


        }
        else
        {
            progressDialog.dismiss();
            Toast.makeText(SettingsActivity.this, "Error Uploading",Toast.LENGTH_SHORT).show();
        }
    }
});

I am using Firebase API in Android Studio. What I am trying to do is to upload an image by the user and save the URL to the database so later I can retrieve it from there. But the method getDownloadUrl() does not return the URL, it returns this string instead:

com.google.android.gms.tasks.zzu@302097b
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shayke
  • 1
  • 1
  • 1
    Please only use the `android-studio` tag for questions about the Android Studio IDE itself. For questions about Android programming in general, use the `android` tag. – Frank van Puffelen Feb 21 '21 at 16:00
  • Determining the download URL requires a call to the server. Because of this, the call to `getDownloadUrl()` returns a `Task` that completes when the download URL comes back from the server. You'll need to call `addSuccessListener()` on it to wait for it to complete. See the documentation [here](https://firebase.google.com/docs/storage/android/download-files#download_data_via_url) and this [answer](https://stackoverflow.com/a/51064689) – Frank van Puffelen Feb 21 '21 at 16:02
  • @FrankvanPuffelen thanks for your response, but I've tried to use the addSuccessListener() and I am getting the same result. Also tried to use the while loop like in the other post. – Shayke Feb 21 '21 at 16:40
  • I'd recommend sticking to the/my accepted answer on that link, and the code from the documentation, neither of which uses a loop. Getting the same results with the code from those places seems highly unlikely, but if you can't get that code to work, update your question to show what you've tried. – Frank van Puffelen Feb 21 '21 at 16:46
  • The question is already marked as solved. Please do not add solution in an edit – Dharman Feb 21 '21 at 17:24

0 Answers0