我想用仿真器的照相机拍照,当我拿一台相机时,它没有保存图片,并给出了一个错误: pickImage方法恢复了XFile类型的未来,我希望返回文件类型,当我使用(作为文件)时,它会给我另一个错误,即Xfile不是文件的子类型。
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),
),
],
),
),
]);
}
}
发布于 2021-12-02 13:59:34
试试下面的XFile代码
final ImagePicker _picker = ImagePicker();
Future getForFrontImage(ImageSource source) async {
final XFile? pickedFile = await _picker.pickImage(source: source);
if (pickedFile != null) {
setState(() {
selectedChequeImg = File(pickedFile.path);
});
}
}
https://stackoverflow.com/questions/70200555
复制相似问题