【问题标题】:Converting of Image File created using image_picker package of flutter to AssetImage/Image.asset?将使用flutter的image_picker包创建的图像文件转换为AssetImage/Image.asset?
【发布时间】:2020-01-21 06:21:15
【问题描述】:

我是 Flutter 新手,我正在构建一个应用程序,我需要将使用 image_picker 包后生成的文件(图像)转换为资产图像以在应用程序中使用。

示例代码如下,创建文件(图片)

final Function onSelectImage;

ImageInput(this.onSelectImage);

File _storedImage;


Future<void> _takePicture() async {

        final imageFile = await ImagePicker.pickImage(
                        source: ImageSource.camera,
                        maxWidth: 600,
        );

        if (imageFile == null) {
                return;
        }

        setState(() {
                _storedImage = imageFile;
        });

        final appDir = await syspaths.getApplicationDocumentsDirectory();

        final fileName = path.basename(imageFile.path);

        final savedImage = await imageFile.copy('${appDir.path}/$fileName');

        widget.onSelectImage(savedImage);
}

提前致谢

【问题讨论】:

    标签: flutter flutter-image


    【解决方案1】:

    您可以创建一个图像变量,您可以在选择图像时恢复和更新该变量。

    见以下代码:

    final Function onSelectImage;
    
    ImageInput(this.onSelectImage);
    
    File _storedImage;
    
    Image _tempImage;
    
    
    Future<void> _takePicture() async {
    
            final imageFile = await ImagePicker.pickImage(
                            source: ImageSource.camera,
                            maxWidth: 600,
            );
    
            if (imageFile == null) {
                    return;
            }
    
            setState(() {
                    _storedImage = imageFile;
            });
    
            final appDir = await syspaths.getApplicationDocumentsDirectory();
    
            final fileName = path.basename(imageFile.path);
    
            final savedImage = await imageFile.copy('${appDir.path}/$fileName');
    
            widget.onSelectImage(savedImage);
            setState(() {
              _tempImage = imageFile;
            });
    
    
    }
    
    
      @override
      Widget build(BuildContext context) {
        return _tempImage == null ? Container(child:null) : Image(image: _tempImage);
      }
    

    【讨论】:

    • @Jaron Dob,感谢您的回答,但我收到以下错误 1._tempImage = imageFile - // 'File' 类型的值不能分配给 'Image' 类型的变量 2。 image:_tempImage // 参数类型'Image'不能赋值给参数类型ImageProvider。
    • 嗨,Sarath,来不及问,但你弄清楚该怎么做了吗?
    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2020-10-03
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多