如果您在使用 Expo 和 Firebase 7.9.0 时遇到无法获取 downloadUrl 的问题,您可以尝试以下方法解决:
npm install firebase
import firebase from 'firebase/app';
import 'firebase/storage';
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID",
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
请确保替换 YOUR_API_KEY
、YOUR_AUTH_DOMAIN
、YOUR_PROJECT_ID
、YOUR_STORAGE_BUCKET
、YOUR_MESSAGING_SENDER_ID
和 YOUR_APP_ID
为您的实际 Firebase 配置。
import * as ImagePicker from 'expo-image-picker';
async function uploadImage() {
const { status } = await ImagePicker.requestCameraRollPermissionsAsync();
if (status !== 'granted') {
console.log('Permission denied');
return;
}
const result = await ImagePicker.launchImageLibraryAsync();
if (!result.cancelled) {
const fileUri = result.uri;
const storageRef = firebase.storage().ref().child('your_folder_name/' + result.fileName);
const snapshot = await storageRef.put(fileUri);
snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log('File available at', downloadURL);
}).catch((error) => {
console.error('Error getting download URL:', error);
});
}
}
请确保替换 your_folder_name
为您的实际文件夹名称。
如果您仍然遇到问题,请检查以下内容:
领取专属 10元无门槛券
手把手带您无忧上云