当用户从他们的相册中选择一张照片时,我想要得到的主要东西是从照片中获得一个url,我可以在我的userdb中使用它。把url放到firebase的用户文档中工作了,ImagePicker也工作了。我的主要问题是我如何(通过firebase存储)获得一个实际的url,而不是像这样:
250 f8b0c - 72 - d3 - 43 - d4 - 9 - cda - b7f6d8a11f81.jpg
代码:
const [image, setImage] = useState("");
//functions
const pickImage = async () => {
try {
// No permissions request is necessary for launching the image library
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
quality: 1,
});
console.log(result);
if (!result.canceled) {
auth.currentUser.photoURL = result.uri
setImage(result.uri);
console.log(result.uri)
imgFirebase()
}
{/**
await updateDoc(doc(db, "users", uid, {
photoURL: result.uri.toString()
}))
*/}
}
catch(E) {
alert(E)
}
};
async function imgFirebase () {
const d = await fetch(image)
const dd = await d.blob()
const fileName = image.substring(image.lastIndexOf("/")+1)
console.log(fileName)
const storage = getStorage();
const storageRef = ref(storage, fileName);
uploadBytes(storageRef,dd).then((snapshot) => {
console.log('Uploaded a blob or file!');
});
const httpsReference = ref(storage, 'https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
}
存储结构firebase存储是什么样子的:**注意,如果我按下图像,它实际上给了我一个新的选项卡的url…我已经尝试了getDownloadUrl函数从文档,但它也给我错误与存储参考
在我的imgFirebase函数什么我需要得到的只是img/blob的url,我从图像选择器/firebase存储
如何获取图片上传后的url
reader.onload = (e) => {
if (file == null) return;
Swal.fire({
title: 'Your uploaded picture',
imageUrl: e.target.result,
imageAlt: 'The uploaded picture'
})
const imageRef = ref(storage, `user/profile/${file.nama + user.uid}`);
uploadBytes(imageRef, file).then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
setImageUrls(url);
});
});
}