从Firebase存储中提取图像的React-Native

时间:2019-12-09 04:44:43

标签: firebase react-native

uploadPhotoAsync = async uri => {
        const path = `photos/${this.uid}/${Date.now()}.jpg`;
    // uploadPhotoAsync = (uri, filename) => {

        return new Promise(async (res, rej) => {
            const response = await fetch(uri);
            const file = await response.blob();

            let upload = firebase
                .storage()
                .ref(path) //path
                .put(file);

            upload.on(
                "state_changed",
                snapshot => {},
                err => {
                    rej(err);
                },
                async () => {
                    const url = await upload.snapshot.ref.getDownloadURL();
                    res(url);
                }
            );
        });
    };

到目前为止,我的工作是根据uid将其保存在Firebase存储中的照片。 因此,当我单击“存储”时,我会从那里看到一个名为“ photos /”的文件夹,其中存在uid文件夹,具体取决于登录的人,以及其中的图像。

问题是我现在应该如何将它们从存储中拉出,以在我的react native应用程序上显示存储在其中的所有图像?

1 个答案:

答案 0 :(得分:0)

        uploadPhotoAsync = async uri => {
        const path = `photos/${this.uid}/${Date.now()}.jpg`;
        // uploadPhotoAsync = (uri, filename) => {

            return new Promise(async (res, rej) => {
                const response = await fetch(uri);
                const file = await response.blob();

                let upload = firebase
                    .storage()
                    .ref(path) //path
                    .put(file);

                    upload.on(
                        "state_changed",
                        snapshot => {},
                        err => {
                            rej(err);
                        },
                        async () => {
                            const url = await upload.snapshot.ref.getDownloadURL();
                            //store this url in firebase db related to the user 
                            //e.g. `${userid}\photo\url` 
                            res(url);
                        }
                    );
                });
            };

您需要存储URL,请参见代码中的注释。 然后,当您需要显示/拉动该图像时,从firebase db获取URL