图像选择器扫描到零抖动

Image picker scanned to null flutter

文件_图片; 最终选择器 = ImagePicker();

Future getImage() async {
  final pickedFile = await picker.getImage(source: ImageSource.camera);

  setState(() {
    if (pickedFile != null) {
      _image = File(pickedFile.path);
    } else {
      print('No image selected.');
    }
  });
}

输出:

D/MediaScannerConnection(16161): Scanned /storage/emulated/0/Android/data/com.xxxx.xxxx/files/Pictures/9d9ed6a1-292c-428a-bf24-38ea1a58742c6940060118053310767.jpg to null

有时会出现此错误,因为您需要清理构建文件夹。尝试执行 flutter clean 并再次重建。

试试这个:

PickedFile pickedFile = await picker.getImage(source: ImageSource.camera);

if (pickedFile == null) {
  return null;
}

Directory appDirectory = await getApplicationDocumentsDirectory();
File newImage = File(appDirectory.path + 'fileName');
newImage.writeAsBytes(File(pickedFile.path).readAsBytesSync());

setState(() {
  _image = newImage;
  print(newImage.path + ' test');
});

所以在深入研究之后,我发现这不是一个错误。