【问题标题】:How to get Download URL from Firebase Storage in flutter如何在颤动中从 Firebase 存储中获取下载 URL
【发布时间】:2021-08-11 14:10:05
【问题描述】:

以下代码用于将任何图像从图库/相机上传到 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);
    }
  }

【问题讨论】:

    标签: flutter dart imagepicker


    【解决方案1】:

    希望你做得很好…… 您可以尝试此方法从 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
    }
    }
    

    【讨论】:

      【解决方案2】:

      我在我的应用程序中使用此功能。使用 getDownloadUrl 传递图像文件并下载。

        Future <String> _uploadphotofile(mFileImage) async {
              final  Reference storageReference = FirebaseStorage.instance.ref().child("products");
              UploadTask uploadTask = storageReference.child("product_$productId.jpg").putFile(imgfile);
          
              String url = await (await uploadTask).ref.getDownloadURL();
              return url;
            }
      

      【讨论】:

        猜你喜欢
        • 2018-12-08
        • 2018-07-04
        • 1970-01-01
        • 2019-11-01
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 2018-03-23
        • 2018-02-04
        相关资源
        最近更新 更多