0

I am currently building a catalog App and these catalogs are basically images that I have saved in firebase storage. I am currently using the code below to make reference to them.

However, these images need to be updated on a weekly basis with new images. This seems to generate a new link for each image when I change the images in the firebase storage. I am maintaining the name and folder location for these particular images despite updating them. How can I implement this in my App without having to change the code "image link" in my App?

Is it possible to make reference the file name that is maintained despite the content being changed? In summary, I would like to make a storage reference that I do not need to change in my app despite me changing the image content in firebase storage? Please share some code snippets if you have such an implementation. Thanks

ViewPager viewPager;
ViewPagerAdapter adapter;

private String[] images = {
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame1.jpg?alt=media&token=b3917686-090f-43fb-852e-0365ac67dd6e",
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame2.jpg?alt=media&token=25e4eb76-5361-42a6-9bfd-6e3b1153611f",
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame3.jpg?alt=media&token=a5d4c8b9-9ecb-4ea1-a095-557955db1d19",
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame4.jpg?alt=media&token=b4598019-13b3-4600-aa49-86fd0a8c5c64",
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame5.jpg?alt=media&token=a4991bfe-d3b4-4f1c-be65-55ddfd1de7ba",
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame6.jpg?alt=media&token=e8b43bb6-c967-495f-b5e2-336f082225b6",
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame7.jpg?alt=media&token=0f135b82-1a05-4585-a511-bbab35a5613a",
        "https://firebasestorage.googleapis.com/v0/b/zamcatalog.appspot.com/o/Game%2Fgame8.jpg?alt=media&token=ab8a9fcf-db2d-4c9c-afd8-969a4db22a8c"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    viewPager = (ViewPager)findViewById(R.id.viewpager);
    adapter = new ViewPagerAdapter(gameActivity.this,images);
    viewPager.setAdapter(adapter);
}

}"

d4rkcon
  • 143
  • 1
  • 8
  • 1
    I don't understand what you mean by "This seems to generate a new link for each image when I change the images in the firebase storage". – Doug Stevenson Jul 27 '18 at 15:44
  • Once a new image is uploaded it has another unique link. Which would require me to change the link in the code above. A store has different catalogs each week. I want to be able to just change the images from firebase storage without altering any code in the APP. – d4rkcon Jul 27 '18 at 15:49
  • So you're saying that when you change a file in storage, the prior download URL that was generated for it no longer works? – Doug Stevenson Jul 27 '18 at 16:37
  • Yes as it will generate a new link – d4rkcon Jul 27 '18 at 17:03
  • I still don't see what you mean by "it will generate a new link". Links are note generated unless you write some code to do so. When are you generating this link? – Doug Stevenson Jul 27 '18 at 17:17
  • The intention is to sync these images in real time on all devices that have the app installed. Catalogs change every week, meaning new images are uploaded to firebase storage in order to sync with user devices. I believe I have to use realtime database and storage to implement this. Just needed guidance on how to do this. As you can see from the code above which is pretty static to those image links. – d4rkcon Jul 27 '18 at 17:31
  • So, where in your code do you generate the links? If you're generating new links every time you replace an image, that seems excessive. Can't you reuse the link from the first time a file is uploaded? – Doug Stevenson Jul 27 '18 at 17:57

1 Answers1

0

I think you should look into passing the cloud storage location (gs://bucket-name/folders...\imageName.jpg) instead of the download URL. This storage location should not change as long as you don't change your file names or folder location. You can use the Firebase storage API to access your files/images using the cloud storage location:

// Create a reference to a file from a Google Cloud Storage URI
StorageReference gsReference = storage.getReferenceFromUrl("gs://bucket/images/stars.jpg");

I highly recommend you read the Cloud Storage for Firebase documentation: https://firebase.google.com/docs/storage/android/start https://firebase.google.com/docs/storage/android/download-files

dizzzzy
  • 136
  • 1
  • 3