As you mentioned, since the images are uploaded from the Firebase console, there is no way, for the app, to know when a new image is loaded and to calculate the corresponding URL.
So this has to be done in the back-end, i.e. on the Firebase platform itself. Cloud Functions for Firebase are especially done for that, see: https://firebase.google.com/docs/functions/
Cloud Functions for Firebase lets you automatically run backend code
in response to events triggered by Firebase features.
In your case, you would write a Cloud Function that would be triggered when a file is loaded to Cloud Storage. This Function would calculate the URL (through the getSignedUrl()
method) and save it to the Firebase database, e.g. Firestore. This way, your Android app can query the database to get the list of URLs (and put them in an ArrayList).
The Cloud Function would be along these lines:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
//See Note below and https://stackoverflow.com/a/50138883/3371862
import * as serviceAccount from 'yourServiceAccount.json';
const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG)
adminConfig.credential = admin.credential.cert(<any>serviceAccount)
admin.initializeApp(adminConfig);
const defaultStorage = admin.storage();
exports.saveSignedURL = functions.storage.object().onFinalize(object => {
const file = defaultStorage.bucket(object.bucket).file(object.name);
const options = {
action: 'read',
expires: '03-17-2025'
};
return file.getSignedUrl(options).then(results => {
const url = results[0];
return admin
.firestore()
.collection('images')
.add({ url: url });
});
});
Note that, in order to use the getSignedUrl()
method, you need to initialize the Admin SDK with the credentials for a dedicated service account, see this SO Q&A: https://stackoverflow.com/a/50138883/3371862
Finally, if you are new to Cloud Functions, you may have a look a this doc page, and in particular at the "Next steps" section at the bottom.