简体   繁体   English

在flutter中从image_picker包中打开相机会导致真实设备上的应用程序崩溃,但在模拟器(android)中运行良好

[英]opening camera from image_picker package in flutter crashes app on real device but works fine in emulator (android)

I am using flutter image_picker: ^0.6.0+17 to get image for a product in app it absolutely works fine on emulator, but on real android device the app crashes and restarts when i click 'open camera' button.我正在使用 flutter image_picker: ^0.6.0+17 在应用程序中获取产品的图像,它在模拟器上绝对可以正常工作,但在真正的 android 设备上,当我单击“打开相机”按钮时,应用程序崩溃并重新启动。

i have tried to use the emulator as same api level as my device i am testing it on redmi 6A android oreo 8.1 no problem is found flutter doctor or flutter analyze selecting image from gallary works fine for both .我曾尝试使用与我的设备相同的 api 级别的模拟器我在 redmi 6A android oreo 8.1 上测试它没有发现问题颤振医生或颤振分析从画廊中选择图像对两者都很好。

import 'dart:io';

import 'package:firstapp/models/product.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImageInput extends StatefulWidget {
  final Function setImage;
  final Product product;

  ImageInput(this.setImage, this.product);

  @override
  State<StatefulWidget> createState() {
    return _ImageInputState();
  }
}

class _ImageInputState extends State<ImageInput> {
  File _imageFile;

  Future _getImage(BuildContext context, ImageSource source) async {
    print('getting image');
    File image = await ImagePicker.pickImage(source: source, maxWidth: 600);
    if(image == null){
      return null;
    }
    setState(() {
      print('file = image');
      _imageFile = image;
    });
    print('setting image');
    widget.setImage(image);
    Navigator.pop(context);
  }

  void _openImagePicker(BuildContext context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            padding: EdgeInsets.all(10),
            height: 150,
            child: Column(children: <Widget>[
              Text(
                'Pick an Image',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                ),
              ),
              SizedBox(
                height: 10,
              ),
              FlatButton(
                textColor: Theme.of(context).primaryColor,
                child: Text('Use Camera'),
                onPressed: () {
                  _getImage(context, ImageSource.camera);
                },
              ),
              FlatButton(
                textColor: Theme.of(context).primaryColor,
                child: Text('Use Gallery'),
                onPressed: () {
                  _getImage(context, ImageSource.gallery);
                },
              )
            ]),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    final buttonColor = Theme.of(context).primaryColor;
    Widget previewImage = Text('Please select an image.');
    if (_imageFile != null) {
      previewImage = Image.file(
        _imageFile,
        height: 300.0,
        width: MediaQuery.of(context).size.width,
        fit: BoxFit.cover,
        alignment: Alignment.center,
      );
    } else if (widget.product != null) {
      previewImage = Image.network(
        widget.product.image,
        height: 300.0,
        width: MediaQuery.of(context).size.width,
        fit: BoxFit.cover,
        alignment: Alignment.center,
      );
    }

    return Column(
      children: <Widget>[
        OutlineButton(
          onPressed: () {
            _openImagePicker(context);
          },
          borderSide: BorderSide(color: buttonColor, width: 1),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Icon(
                Icons.camera_alt,
                color: buttonColor,
              ),
              SizedBox(
                width: 5.0,
              ),
              Text(
                'Add Image',
                style: TextStyle(color: buttonColor),
              )
            ],
          ),
        ),
        SizedBox(
          height: 10,
        ),
        previewImage,
      ],
    );
  }
}

these are the debug log after i press use camera and then app restarts这些是我按下使用相机然后应用程序重新启动后的调试日志

I/flutter (20143): getting image
D/Surface (20143): Surface::disconnect(this=0x9577d000,api=1)
D/GraphicBuffer(20143): unregister, handle(0xaa60e900) (w:720 h:1440 s:736 f:0x1 u:b00)
D/GraphicBuffer(20143): unregister, handle(0xaa610280) (w:720 h:1440 s:736 f:0x1 u:b00)
D/GraphicBuffer(20143): unregister, handle(0xaa610640) (w:720 h:1440 s:736 f:0x1 u:b00)
D/Surface (20143): Surface::disconnect(this=0x9577d000,api=-1)
D/Surface (20143): Surface::disconnect(this=0x9577c000,api=1)
D/GraphicBuffer(20143): unregister, handle(0x94f956c0) (w:720 h:1440 s:736 f:0x1 u:b00)
V/PhoneWindow(20143): DecorView setVisiblity: visibility = 4, Parent = ViewRoot{f21fea2 com.example.firstapp/com.example.firstapp.MainActivity,ident = 0}, this = DecorView@5af733[MainActivity]
Lost connection to device.
Exited (sigterm)

these is the simple code i am using , just change source with ImageSource.camera from image picker package这些是我正在使用的简单代码,只需使用图像选择器包中的ImageSource.camera更改source

Future _getImage(BuildContext context, ImageSource source) async {
    print('getting image');
    File image = await ImagePicker.pickImage(source: source, maxWidth: 600);
    if(image == null){
      return null;
    }
    setState(() {
      print('file = image');
      _imageFile = image;
    });
    print('setting image');
    widget.setImage(image);
    Navigator.pop(context);
  }

Thanks in advance提前致谢

Update, with the new Version of image_picker this issue has been solved.更新,使用新版本的 image_picker 这个问题已经解决。

Please follow official docs of https://pub.dev/packages/image_picker for more reference.请关注https://pub.dev/packages/image_picker的官方文档以获得更多参考。

In runner Info.plist file, add these works for me在 runner Info.plist 文件中,为我添加这些作品

<string>Need to upload image</string>
<key>NSCameraUsageDescription</key>
<string>Need to upload image</string>
<key>NSMicrophoneUsageDescription</key>
<string>Need to upload image</string>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打开相机时颤振image_picker崩溃 - Flutter image_picker crashes when opening camera Android应用程式可在模拟器上正常运作,但在真实装置中当机 - Android app works fine in emulator but crashes in real device flutter 和应用程序崩溃的 image_picker 插件 - image_picker plugin for flutter and app crashes Android 应用程序在少数真实设备上崩溃,但在与真实设备具有相同 API 的模拟器上运行良好 - Android app crashes on few real devices but works fine on an emulator which has the same API as that of the real device android应用程序在模拟器上工作正常,但在实际设备上崩溃,并在android studio中出现以下错误,并附加了代码 - android app works fine on emulator but crashes on real device with following error in android studio the code is attached Flutter:相机的图像选择器方法在应用程序的调试版本(在设备上)中工作正常,但在发布版本运行时崩溃(来自 Google Play) - Flutter: Image Picker method for camera works ok in debug version of app (on device), but then crashes when release version run (from Google Play) Flutter 电话身份验证在模拟器上工作正常,但在真实设备上不工作(Android) - Flutter Phone authentication works fine on emulator but not working on real device (Android) 取消 (image_picker) 活动时,Flutter 在 Android 上崩溃 - Flutter crashes on Android when canceling (image_picker) activity 应用程序在模拟器上运行正常,但在真实设备上崩溃 - App runs fine on emulator, but crashes on real device 应用程序在模拟器上运行良好,但在真实设备上崩溃 - App working fine on emulator but crashes on real device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM