I'm a new member. I'm also relatively new to java and, in the last few weeks, firebase. I'm trying to stumble my way through the codelabs frebase android tutorial and have got as far as the very last updated required in "part 8" of the lesson at https://codelabs.developers.google.com/codelabs/firebase-android/#7
At the end of part 8 we are requested to add a "putImageInStorage" method into MainActivity.java
private void putImageInStorage(StorageReference storageReference, Uri uri, final String key) {storageReference.putFile(uri).addOnCompleteListener(MainActivity.this,
new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
FriendlyMessage friendlyMessage =
new FriendlyMessage(null, mUsername, mPhotoUrl,
task.getResult().getMetadata().**getDownloadUrl()**
.toString());
mFirebaseDatabaseReference.child(MESSAGES_CHILD).child(key)
.setValue(friendlyMessage);
} else {
Log.w(TAG, "Image upload task was not successful.",
task.getException());
}
}
});
}
However, when i ran this as instructed the IDE would not compile, citing the following section as being the problem: ".getDownloadUrl()" I have spent several hours looking around for a solution, and it appears this has been "deprecated". I did try to swap out the line of code as suggested at getDownloadUrl() of firebase storage return the same and wrong link for all the uploaded files. How to fix this? (ie use * task.getMetadata().getReference().getDownloadUrl().toString() * instead of - * task.getResult().getStorage().getDownloadUrl().toString(), but this did not appear to work. I then tried running this method i'd found on stackoverflow, but it now crashes my app("Unfortunately, Friendly Chat has stopped")
private void putImageInStorage(final StorageReference storageReference, Uri
uri, final String key) {
storageReference.putFile(uri).addOnCompleteListener(
new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
FriendlyMessage friendlyMessage =
new FriendlyMessage(null, mUsername, mPhotoUrl,
task.getResult().getStorage().getDownloadUrl().toString() , mFirebaseUser.getUid());
mFirebaseDatabaseReference.child(MESSAGES_CHILD).child(key)
.setValue(friendlyMessage);
} else {
Log.w(TAG, "Image upload task was not successful.",
task.getException());
}
}
});
}
Any solutions, especially explained for an absolute novice, would be very gratefully received. Thank you for your time to read this, and any help.