I am working on an Android app for taking photos. To start with the user takes a profile picture. The Url for this image gets saved in Firebase Realtime database. Sample code for retrieving downloadUrl below
Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
uploadTask = riversRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});
This url gets saved in multiple places in database where I need to load the profile picture of the user - entry for each of his posts, his messages to other people on the app, etc.
Now if he changes the profile picture at a later date, I have the following 2 options
- Update each and every database entry - very cumbersome, brute force and resource intensive
- Overwrite the original profile picture - easier but I am unable to figure out how. Reason is that even for same filename, Firebase Storage generates a new URL for each download, defeating the purpose of this exercise.
So is there a better way to upload an image to Firebase storage and generate a URL which is static?
Reason for this dependancy is that I am loading images using Picasso and Glide and I need the url to be static.
Out-of-the-box ideas are also welcome as long as they solve my problem of managing profile picture with least effort.