在 Flutter 中从图库或相机中选择图像

A T*_*ice 2 flutter flutter-dependencies

我试图为用户提供一种从他们的画廊或他们的相机添加照片的方法,但是,按下按钮后,颤动不断崩溃。

我已经实现了图像选择器依赖项以及 Dart:io,迁移到了 Android X 但是,仍然没有运气。这是我的一些代码:

class _PreferencesState extends State<_Preferences>
    with TickerProviderStateMixin {


File _image;
getImage(bool isCamera) async {
   File image;

if (isCamera) {
  image = await ImagePicker.pickImage(source: ImageSource.camera);
} else {
  image = await ImagePicker.pickImage(source: ImageSource.gallery);
}

setState(() {
  _image = image;
  });
}
Run Code Online (Sandbox Code Playgroud)

然后在这里调用:

  FlatButton(
      textColor: Theme.of(context).primaryColor,
      child: Text('Use Camera'),
      onPressed: () {
          getImage(true);
      },
  ),
  _image == null
      ? Container()
      : Image.file(
          _image,
          height: 300,
          width: 300,
      ),    
Run Code Online (Sandbox Code Playgroud)

每次我调用该方法时,都会收到此错误:

发生异常。PlatformException (PlatformException(error, Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' 在空对象引用上,空值))

我以为我将图像类型从 null 更改为不会被调用,但是,我不确定在这里还能做什么。

Dar*_*han 5

崩溃可能是由于相机许可。Image picker插件已删除相机权限条件。当我们访问相机时,应用程序要求我们获得许可,但不再支持此条件。我们需要使用permission_handler插件手动询问权限。了解更多详细信息,关于这个变化在这里,看到这个关于如何添加使用运行时允许链接permission_handler。希望这能解决您的问题。