通过flutter在firebase存储中上传图像

发布于 2025-02-05 04:22:25 字数 753 浏览 3 评论 0原文

我尝试在Firebase存储上上传图像并获取错误。

Future selectImage(ImageSource source) async {
    try {
      XFile? xImage = await ImagePicker().pickImage(source: source);//getting picture from phone
      if(xImage == null ) return 'there is no image';
      final File file = File(xImage.path);
      //final Image image = Image.file(File(xImage.path));

      print(xImage.path);
      Directory appDocDir = await getApplicationDocumentsDirectory();
      String filePath = '${appDocDir.absolute}/file-to-upload.png';
      print(filePath);

      final storageRef = FirebaseStorage.instance.ref().child("UsersProfilePhoto/");

      await storageRef.putFile(file);
    } on FirebaseException catch (e) {
      print(e.message);
    }
  }

I try to upload an image on firebase storage and get the error.

Future selectImage(ImageSource source) async {
    try {
      XFile? xImage = await ImagePicker().pickImage(source: source);//getting picture from phone
      if(xImage == null ) return 'there is no image';
      final File file = File(xImage.path);
      //final Image image = Image.file(File(xImage.path));

      print(xImage.path);
      Directory appDocDir = await getApplicationDocumentsDirectory();
      String filePath = '${appDocDir.absolute}/file-to-upload.png';
      print(filePath);

      final storageRef = FirebaseStorage.instance.ref().child("UsersProfilePhoto/");

      await storageRef.putFile(file);
    } on FirebaseException catch (e) {
      print(e.message);
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人│生佛魔见 2025-02-12 04:22:27

尝试这样。

Future selectImage(ImageSource source) async {
        try {
          ImagePicker imagePicker = ImagePicker();
          XFile pickedFile = await imagePicker.pickImage(source: source,   imageQuality: 80);
          File imageFile = File(pickedFile.path);
          if(imageFile == null ) return 'there is no image';
          print(imageFile.path);
          String fileName = DateTime.now().millisecondsSinceEpoch.toString();
    
          final storageRef = FirebaseStorage.instance.ref().child("UsersProfilePhoto/");
    
          await storageRef.putFile(imageFile);
        } on FirebaseException catch (e) {
          print(e.message);
        }
      }

Try like this.

Future selectImage(ImageSource source) async {
        try {
          ImagePicker imagePicker = ImagePicker();
          XFile pickedFile = await imagePicker.pickImage(source: source,   imageQuality: 80);
          File imageFile = File(pickedFile.path);
          if(imageFile == null ) return 'there is no image';
          print(imageFile.path);
          String fileName = DateTime.now().millisecondsSinceEpoch.toString();
    
          final storageRef = FirebaseStorage.instance.ref().child("UsersProfilePhoto/");
    
          await storageRef.putFile(imageFile);
        } on FirebaseException catch (e) {
          print(e.message);
        }
      }
清泪尽 2025-02-12 04:22:27

尝试一下

    import 'package:path/path.dart' as Path1;//for storing image path in firestore
    
    DatabaseReference reference = FirebaseDatabase.instance.reference();
    FirebaseStorage storage = FirebaseStorage.instance;
    File? fileImage;
    
    //call this function for picking image
    pickedImage() async{
        PickedFile? file = await ImagePicker().getImage(
            source: ImageSource.camera,
            maxHeight: 1000,
            maxWidth: 1000
        );
    
        File image = file!.path;
       
        setState((){
          fileImage = image;
        });
    
      }
    
//call this function for storing image in firestore
     _storeImage() async{
        Reference storagerefrence = storage.ref().child('card_images/${Path1.basename(fileImage!.path)}');
        TaskSnapshot uploadTask = await storagerefrence.putFile(fileImage!);
        String url = await storagerefrence.getDownloadURL();//download url from firestore and add to firebase database
    
        reference.child("image").push().set({
          'image' : url
        });
    }

Try this

    import 'package:path/path.dart' as Path1;//for storing image path in firestore
    
    DatabaseReference reference = FirebaseDatabase.instance.reference();
    FirebaseStorage storage = FirebaseStorage.instance;
    File? fileImage;
    
    //call this function for picking image
    pickedImage() async{
        PickedFile? file = await ImagePicker().getImage(
            source: ImageSource.camera,
            maxHeight: 1000,
            maxWidth: 1000
        );
    
        File image = file!.path;
       
        setState((){
          fileImage = image;
        });
    
      }
    
//call this function for storing image in firestore
     _storeImage() async{
        Reference storagerefrence = storage.ref().child('card_images/${Path1.basename(fileImage!.path)}');
        TaskSnapshot uploadTask = await storagerefrence.putFile(fileImage!);
        String url = await storagerefrence.getDownloadURL();//download url from firestore and add to firebase database
    
        reference.child("image").push().set({
          'image' : url
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文