1
votes

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

  1. Update each and every database entry - very cumbersome, brute force and resource intensive
  2. 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.

1

1 Answers

0
votes

I eventually decided to do away with the URLs altogether. The google storage Uri with the prefix gs:// is static. And only accessible from within the app. So I decided to write a custom RequestHandler for the gs:// uri using guidance from this tutorial

Now every time the picture is updated, I overwrite the original file and it automatically updates the image for all the links across all the entries in the database.