首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将Xfile转换为文件?

如何将Xfile转换为文件?
EN

Stack Overflow用户
提问于 2021-12-02 13:41:42
回答 1查看 1.6K关注 0票数 0

我想用仿真器的照相机拍照,当我拿一台相机时,它没有保存图片,并给出了一个错误: pickImage方法恢复了XFile类型的未来,我希望返回文件类型,当我使用(作为文件)时,它会给我另一个错误,即Xfile不是文件的子类型。

代码语言:javascript
运行
复制
class ImageInput extends StatefulWidget {
  final Function? onSelect;
  const ImageInput({Key? key, this.onSelect}) : super(key: key);

  @override
  _ImageInputState createState() => _ImageInputState();
}

class _ImageInputState extends State<ImageInput> {
  File? _imagePicker;

  Future<void> _takeAPicture() async {
    final picker = ImagePicker();
    final imageFile = await picker.pickImage(
      source: ImageSource.gallery,
      maxWidth: 600,
    );
  }
  

  Future<void> _chooseAPicture() async {
    final picker = ImagePicker();
    final imageFile = await picker.pickImage(
      source: ImageSource.camera,
      maxWidth: 600,
    ) as File;
    setState(() {
      _imagePicker = File(imageFile.path);
    });
    final appdir = await syspath.getApplicationDocumentsDirectory();
    final fileName = path.basename(imageFile.path);
    final savedImage = await imageFile.copy('${appdir.path}/${fileName}');
    widget.onSelect!(savedImage);
  }

  @override
  Widget build(BuildContext context) {
    return Row(children: [
      Container(
        child: _imagePicker != null
            ? Image.file(
                _imagePicker as File,
                fit: BoxFit.cover,
                width: double.infinity,
              )
            : Text(
                'No Image Taken',
                textAlign: TextAlign.center,
              ),
        alignment: Alignment.center,
        height: 100,
        width: 100,
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey, width: 1),
        ),
      ),
      SizedBox(
        height: 10,
      ),
      Expanded(
        child: Column(
          children: [
            TextButton.icon(
              onPressed: () => _takeAPicture(),
              label: Text('choose an Image'),
              style: ButtonStyle(
                textStyle: MaterialStateProperty.all(
                  TextStyle(color: Theme.of(context).primaryColor),
                ),
              ),
              icon: Icon(Icons.camera),
            ),
            TextButton.icon(
              onPressed: () => _chooseAPicture(),
              label: Text('Take an Image'),
              style: ButtonStyle(
                textStyle: MaterialStateProperty.all(
                  TextStyle(color: Theme.of(context).primaryColor),
                ),
              ),
              icon: Icon(Icons.camera_enhance_sharp),
            ),
          ],
        ),
      ),
    ]);
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-12-02 13:59:34

试试下面的XFile代码

final ImagePicker _picker = ImagePicker();

代码语言:javascript
运行
复制
Future getForFrontImage(ImageSource source) async {
    final XFile? pickedFile = await _picker.pickImage(source: source);
    if (pickedFile != null) {
      setState(() {
        selectedChequeImg = File(pickedFile.path);
      });
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70200555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档