Solution 1 :
I guess you need to get it directly from storageReference , so do as following :
Uri downloadUrl = filePath.getDownloadUrl();
Problem :
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();
}
}
});
Comments
Comment posted by firebase.google.com/docs/storage/android/…
You’re not using the API correctly. getDownloadUrl returns a Task that gives you the URL asynchronously. I suggest reviewing the documentation.
Comment posted by Ekta Dushanj
Could you please add the exception you are getting.
Comment posted by Doug Stevenson
That won’t work either.
Comment posted by Taki
did you log the downloadUrl , are you getting null value ?
Comment posted by Taki
it worked for me because i m working on a project with firebase and its components too
Comment posted by Doug Stevenson
Trust me, it won’t work. You will get a string, but it won’t be a download URL string.
Comment posted by pastebin.com/dpYaFwcp
Trust me it does work and here is my code for you