首页 > 解决方案 > 如何在颤动中将图像临时存储在文件中?

问题描述

我想临时存储图像并获取文件路径,因此我可以将图像存储在 firebase 中,然后删除临时文件。我看到一些帖子将文件中的图像保存为字节。但是我将如何将它保存在 Firebase 存储中呢?有什么建议么?

Container(
                    child: SfSignaturePad(
                      key: _signaturePadKey,
                      minimumStrokeWidth: 1,
                      maximumStrokeWidth: 3,
                      strokeColor: Colors.blue,
                      backgroundColor: Colors.grey,
                    ),
                    height: 200,
                    width: 300,
                  ),


                  Row(
                      children: [
Text('clear image', style: kTextFieldReg,),
                        SizedBox(height:4, width: 4 ),
                        IconButton(onPressed: ()

                           async {
                             _signaturePadKey.currentState.clear();

                          // Navigator.push(context, MaterialPageRoute(
                          //     builder: (context) => ScreenTwo()));
                        }, icon: CircleAvatar(
                            backgroundColor: Color(0xff660B21),
                            radius: 19,
                            child: ClipOval(

                              child:Icon((Icons.close), color: Colors.white, ))
                        )) ]


                  ),
                  SizedBox(width: 20, height: 20,),
                  ElevatedButton(
child: Text(
  'Add',
  style: TextStyle(color: Colors.white),
),
                    style: ElevatedButton.styleFrom(
                        minimumSize: Size(150, 60), primary: Color(0xff660B21),),
                    onPressed: () async{
           // NEED TO STORE THIS IMG TEMPORARILY ANS GET ITS FILE PATH
                      ui.Image image = await _signaturePadKey.currentState.toImage(pixelRatio: 3);
                      
                      if (_formKey.currentState.validate()) ...

标签: firebaseflutterfirebase-storage

解决方案


当您将文件保存到临时文件时,您只需要获取path文件和以下代码:

Future<void> uploadFile(String filePath) async {
  File file = File(filePath);

  try {
    await firebase_storage.FirebaseStorage.instance
        .ref('uploads/file-to-upload.png')
        .putFile(file);
  } on firebase_core.FirebaseException catch (e) {
    // e.g, e.code == 'canceled'
  }
}

有关如何将文件保存到临时路径的更多详细信息,我们需要您提供更多信息。上面的代码应该是将文件从路径上传到 firebase 存储的部分。


推荐阅读