flutter 当从图像选择器打开摄像头时,应用程序“与设备的连接丢失”,

oxosxuxt  于 2023-01-27  发布在  Flutter
关注(0)|答案(5)|浏览(163)

当我打开相机使用图像选择器设置个人资料图像时,应用程序崩溃,我失去了与设备的连接

从多媒体资料中选取图像时,图像工作正常,

我的pubspec.yaml

name: flutterapp
description: A new Flutter project.

# Prevent accidental publishing to pub.dev.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  
  connectivity: ^3.0.6
  flutter:
    sdk: flutter
 
  flutter_localizations:
    sdk: flutter
  font_awesome_flutter: 
  geolocator: ^7.7.0
  http: ^0.13.4
  
  image_picker: ^0.8.4+3
  local_auth: ^1.1.8
  path_provider: ^2.0.5
  pdf: ^3.6.0
  permission_handler: ^8.2.5
  shared_preferences: ^2.0.8
  

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^1.0.0

flutter:
  uses-material-design: true
  
  # Enable generation of localized Strings from arb files.
  generate: true

  assets:
    # Add assets from the images directory to the application.
    - assets/
jq6vz3qz

jq6vz3qz1#

如果你使用的是iOS设备,那么你必须添加图像拾取的权限。将这些行添加到你的info.plist文件中。

<key>NSCameraUsageDescription</key>
<string>Access to take a photo by camera</string>
<key>NSAppleMusicUsageDescription</key>
<string>Access to pick a photo</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Access to pick a photo</string>
ycl3bljg

ycl3bljg2#

我有同样的问题,我检查了内存的状态,是的,这种情况发生beacuse内存内存ram的设备是满的,并关闭任务自动,所以。
1.我检查了flutter的observatory中的状态存储器,当应用程序运行时,您按“Ctrl + Shift + P”并写入observatory,以查看设备enter image description hereenter image description here的存储器状态
1.我用Image.file小部件的属性定义了cacheHeight和cacheWidth,并在none中修改了filterQuality属性:
图像.file(文件(路径),过滤器质量:过滤器质量。无,缓存高度:66,缓存宽度:50、)
原谅我的英语。

jecbmhm3

jecbmhm33#

在另一台设备上运行您的代码,然后它可以正常工作。我第一次使用'小米Note 7 pro',但显示相同的错误后,在另一台设备上运行代码,它的工作正常。此外,您可以更新您的image_picker的版本。
它对我很有效,我使用了:
图像选择器(_P):^0.8.4+1
扑动版本:2.2.3

void _pickImageCamera() async {
final picker = ImagePicker();
final pickedImage =
await picker.pickImage(source: ImageSource.camera, imageQuality: 90);
final pickedImageFile = File(pickedImage!.path);
setState(() {
  _pickedImage = pickedImageFile;
});
Navigator.pop(context); }
zpqajqem

zpqajqem4#

尝试下面的代码,希望它对你有帮助。在下面的代码中,我用相机以及画廊挑选的图像
dart.io包声明文件类型

File? imagePicked;

创建从Gallery中选取图像的功能

void gallaryImage() async {
    final picker = ImagePicker();
    final pickedImage = await picker.pickImage(
      source: ImageSource.gallery,
    );
    final pickedImageFile = File(pickedImage!.path);
    setState(() {
      imagePicked = pickedImageFile;
    });
  }

创建从摄像机拾取图像的函数

void cameraImage() async {
    final picker = ImagePicker();
    final pickedImage = await picker.pickImage(
      source: ImageSource.camera,
    );
    final pickedImageFile = File(pickedImage!.path);
    setState(() {
      imagePicked = pickedImageFile;
    });
  }

创建您的小部件

Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextButton.icon(
          onPressed: cameraImage,
          label: Text(
            'Camera',
            style: TextStyle(
              color: Colors.black,
            ),
          ),
          icon: Icon(
            Icons.camera,
          ),
        ),
        TextButton.icon(
          onPressed: gallaryImage,
          label: Text(
            'Gallery',
            style: TextStyle(
              color: Colors.black,
            ),
          ),
          icon: Icon(Icons.image),
        ),
      ],
    ),
qmelpv7a

qmelpv7a5#

大家好,我遇到了同样的问题,由于我的应用程序无法从崩溃的地方打开到同一个屏幕,我不得不遵循以下官方方法
https://docs.flutter.dev/cookbook/plugins/picture-using-camera

相关问题