如何使用图像提供程序将捕获的图像或从图库中选择的图像保存到资产文件夹中?

How to save captured or image that selected from gallery using image provider, in to assets folder?

我想将从以下功能捕获的图像保存到应用程序资产文件夹中。

_openGallery(BuildContext context) async{
          var picture = await ImagePicker.pickImage(source: ImageSource.gallery);

          this.setState((){
            imageFile=picture;
          });
          Navigator.of(context).pop();
        }

        _openCamera(BuildContext context) async {
          var picture = await ImagePicker.pickImage(source: ImageSource.camera);
          this.setState((){
            imageFile=picture;
          });
          Navigator.of(context).pop();
        }

我试过这个代码

Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path;
File newImage = await _image.copy('$path');

但它给了我这个错误 OS 错误:没有这样的文件或目录,errno = 2 flutter..有人可以帮忙解决这个问题吗。

尝试为您的文件命名

Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path+'/my_image.png';
File f = File(path)
f.writeAsBytes(yourImage.asBytes);

这对我有用

  Directory pathd = await getApplicationDocumentsDirectory();
  String path =pathd.path;
  print("$path");
  final File newImage = await imageFile.copy('$path/name.jpg');