gpt4 book ai didi

flutter - 如何在 flutter 中从 Firebase 存储获取下载 URL

转载 作者:行者123 更新时间:2023-12-04 16:37:35 27 4
gpt4 key购买 nike

以下代码用于将任何图像从画廊/相机上传到 Firebase 存储。我成功地将图像与元数据一起上传到存储。现在的问题是我无法获取上传图像的下载 URL。尝试了很多,但没有找到任何解决方案。

FirebaseStorage storage = FirebaseStorage.instance;
final picker = ImagePicker();
PickedFile pickedImage;
File imageFile;

Future<void> _upload(String inputSource) async {
try {
pickedImage = await picker.getImage(
source: inputSource == 'camera'
? ImageSource.camera
: ImageSource.gallery,
maxWidth: 1920);

final String fileName = path.basename(pickedImage.path);
imageFile = File(pickedImage.path);

try {
// Uploading the selected image with some custom meta data
await storage.ref(fileName).putFile(
imageFile,
SettableMetadata(
customMetadata: {
'uploaded_by': 'A bad guy',
'description': 'Some description...'
},
),
);

// Refresh the UI
setState(() {});
} on FirebaseException catch (error) {
print(error);
}
} catch (err) {
print(err);
}
}

最佳答案

希望你做得很好 …
您可以尝试使用此方法从 Firebase 存储获取图像(任何文件)的 URL 到 Firebase 存储,然后您可以检索图像。

class _UploadAdState extends State<UploadAdPage> {
final formKey = GlobalKey<FormState>();
File _myimage;
String imgUrl;

Future getImage1(File chosenimage) async {
PickedFile img =
await ImagePicker.platform.pickImage(source: ImageSource.gallery);

if (chosenimage == null) return null;

File selected = File(img.path);
setState(() {
_myimage = chosenimage;
});
}

// changing the firestore rules and deleteing if request.auth != null;
sendData() async {
// to upload the image to firebase storage
var storageimage = FirebaseStorage.instance.ref().child(_myimage.path);
UploadTask task1 = storageimage.putFile(_myimage);

// to get the url of the image from firebase storage
imgUrl1 = await (await task1).ref.getDownloadURL();
// you can save the url as a text in you firebase store collection now
}
}

关于flutter - 如何在 flutter 中从 Firebase 存储获取下载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67652274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com