【问题标题】:Flutter app crashing when trying to display more than 10 images尝试显示超过 10 个图像时,Flutter 应用程序崩溃
【发布时间】:2019-11-08 16:09:33
【问题描述】:

我正在编写我的第一个 Flutter 应用程序。该应用程序允许用户拍摄多张图像(从 1 到 50+),并使用 ListView 将每张图像一次全部显示在屏幕上。

我遇到的问题是,应用程序在三星 SM A520F 上拍摄大约 10/12 张图片后崩溃,我猜这是因为这不是一个非常强大的设备。

有没有一种方法可以显示图像的缩略图而不是加载完整尺寸的图像?

错误信息: 我实际上没有收到任何错误消息,应用程序似乎只是重新启动!

这是我的代码

import 'package:flutter/material.dart';
import 'package:myapp/utilities/app_constants.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:gallery_saver/gallery_saver.dart';

class FormCameraField extends StatefulWidget {
  final InputDecoration decorations;
  final Map field;
  // functions
  final Function onSaved;
  final Function onFieldSubmitted;

  FormCameraField({
    this.decorations,
    @required this.field,
    @required this.onSaved,
    @required this.onFieldSubmitted,
  });

  @override
  _FormCameraFieldState createState() => _FormCameraFieldState();
}

class _FormCameraFieldState extends State<FormCameraField> {
  List<File> images = [];

  Future<void> _takePhoto(ImageSource source) async {
    ImagePicker.pickImage(source: source, imageQuality: 90).then(
      (File recordedImage) async {
        if (recordedImage != null && recordedImage.path != null) {
          try {
            // store image to device gallery
            if (widget.field["StoreCaptureToDevice"] == true) {
              GallerySaver.saveImage(recordedImage.path,
                  albumName: kAppName.replaceAll(" ", "_"));
            }

            setState(() {
              images.add(recordedImage);
            });
          } catch (e) {
            print("ERROR SAVING THE FILE TO GALLERY");
            print(e);
          }
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Expanded(
              child: MaterialButton(
                child: Text("Take Photo"),
                onPressed: () async {
                  await _takePhoto(ImageSource.camera);
                },
              ),
            ),
            Expanded(
              child: MaterialButton(
                child: Text("Select Photo"),
                onPressed: () async {
                  await _takePhoto(ImageSource.gallery);
                },
              ),
            ),
          ],
        ),
        ListView.builder(
          shrinkWrap: true,
          physics: ClampingScrollPhysics(),
          itemCount: images.length,
          itemBuilder: (BuildContext context, index) {
            return Container(
              child: Image.file(
                images[index],
              ),
            );
          },
        )
      ],
    );
  }
}


【问题讨论】:

  • 您找到解决方案了吗?当我打开大约 5 MB 的图像并将其质量降低到 25% 时,我遇到了同样的问题
  • 嗨@SarthakSinghal 我找到了一个临时解决方案 - 我基本上创建了图像的缩略图,然后显示缩略图而不是完整图像。
  • 谢谢。这就是我所做的xd

标签: image flutter camera


【解决方案1】:

有一个类似的问题。用较小(~50kb)的图像替换我的图像似乎工作正常,所以我认为你是对的,在功能较弱的设备上加载所有这些图像似乎是问题所在。 pub.dev 上有一个 image 包应该可以解决问题。我正在使用 Firebase,所以我会更倾向于他们新的调整大小图像扩展。祝你好运,让我们知道它是否有效

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。

    这是因为某些图片导致 Flutter 引擎崩溃。 最后一期在这里https://github.com/flutter/flutter/issues/73767

    在ios上总是导致崩溃的示例图片在这里 https://github.com/flutter/flutter/issues/73932

    所以,等待 Flutter 团队修复错误。

    【讨论】:

    • 仍然面临这个
    • 你用什么 Flutter 版本?
    【解决方案3】:

    我也有同样的问题。这实际上是颤振中的一个错误。他们目前正在努力修复下一个稳定版本中的错误。

    【讨论】:

    • 嗯,哪个来源?
    • 我在 1.5 年后有任何更新现在面临这个问题吗?
    【解决方案4】:

    我创建了缩略图,这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      相关资源
      最近更新 更多