Flutter Image Picker 仅适用于模拟器

问题描述

这里需要一些帮助 - Flutter 相对较新。我正在尝试构建一个图像选择器,它允许用户从他们的图库中选择一个图像,一旦选择它就会显示在应用程序中。它在模拟器中按预期工作,但在我测试过的两个设备(Android API 26 和 28)上,用户可以打开图库,但是一旦选择了图像,图像就不会保存在应用程序内屏幕上.此后,选择图像按钮变得无响应。我在一个单独的程序中重新编写了我的代码,发现它工作得很好。可能是 android 构建依赖项或权限中的某些内容

     File _image;
 final picker = ImagePicker();


 Future getimage() async {
   /* Gets the image from gallery or Camera */
   final pickedFile = await picker.getimage(source: ImageSource.gallery); //gallery
    // final pickedFile = await picker.getimage(source: ImageSource.camera); //camera
   setState(() {
     if (pickedFile != null) {
       _image = File(pickedFile.path);
     } else {
       print('No image selected.');
     }
   });
 }
Container(
        margin: EdgeInsets.symmetric(horizontal: 20,),color: Colors.grey[800],padding: EdgeInsets.symmetric(vertical: 20,horizontal: 10),child: SingleChildScrollView(
          child: Column(
            children:<Widget>[
              Center(
                child: GestureDetector(
                  onTap: () => getimage(),child: (_image == null)
                   ? Container(
                    height: 150,width: 150,child: Icon(
                            MdiIcons.cameraPlus,size: 40
                        ),decoration: Boxdecoration(
                      borderRadius: BorderRadius.circular(25),color: Colors.white
                    ),)
                  : Container(
                    height: 180,width: 130,child: Image.file(_image),

解决方法

试试这个代码

 Container(
            margin: EdgeInsets.symmetric(horizontal: 20,),color: Colors.grey[800],padding: EdgeInsets.symmetric(vertical: 20,horizontal: 10),child: SingleChildScrollView(
              child: Column(
                children:<Widget>[
                  Center(
                    child: GestureDetector(
                      onTap: () => getImage(),child: (_image == null)
                       ? Container(
                        height: 150,width: 150,child: Icon(
                                MdiIcons.cameraPlus,size: 40
                            ),decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(25),color: Colors.white
                        ),)
                      : Container(
                        height: 180,width: 130,decoration: BoxDecoration(
                              image: DecorationImage(
                                fit: BoxFit.cover,image: FileImage(_image)
                              )
                            ),
,

图像选择器在我的应用中运行良好。试试这个代码,看看:

class _ProfileImageGetterState extends State<ProfileImageGetter> {
  File _image;
  final picker = ImagePicker();

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

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

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 15.0),child: _image == null
          ? GestureDetector(
              onTap: getImage,child: Container(
                decoration: BoxDecoration(
                  shape: BoxShape.circle,boxShadow: [
                    BoxShadow(
                        blurRadius: 0.5,color: kGreyColor,spreadRadius: 0.5)
                  ],child: CircleAvatar(
                  child: Image.asset("images/edit-image-pic.png"),backgroundColor: kAccentColor,radius: 50.0,)
          : GestureDetector(
              onTap: getImage,boxShadow: [
                    BoxShadow(blurRadius: 3,spreadRadius: 1)
                  ],child: CircleAvatar(
                  backgroundImage: FileImage(_image),child: Image.asset("images/edit-image-pic.png"),);
  }
}