文件选择器 png 格式图像出现问题

发布于 2025-01-10 02:30:05 字数 2316 浏览 0 评论 0原文

我在应用程序中使用 ImagePicker 来选择和上传图像,但它最近开始出现错误,并且在选择 png 格式图像时不断出现故障。

因此,我切换到文件选择器。但它只能起到一定的作用,而且我的应用程序仍然卡住了。我只能看到它的显示,不幸的是图像没有存储在后端(jpg 和 jpeg 图像工作正常)。

这是图像选择器代码(如果有使用此包上传 png 图像的解决方法,我们将不胜感激):

final ImagePicker _picker = ImagePicker();

  Future imageSelectorGallery() async {
    var image = (await _picker.pickImage(
      source: ImageSource.gallery,
    ));
    if (image != null) {
      Uint8List imageBytes = await image
          .readAsBytes(); // A fixed-length list of 8-bit unsigned integers which is the file read as bytes
      String baseimage = base64Encode(imageBytes);
      if (mounted) setState(() {});
      post = baseimage;
      Navigator.push(context,MaterialPageRoute(builder: (context) => CreatePosts(post,user,caption,upvotes)));
    }
  }

这是我已经实现的文件选择器代码,任何帮助找出这里的错误也将不胜感激

Future imageSelectorGallery() async {
    FilePickerResult? image = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
    );
    if (image != null) {
      Uint8List? imageBytes = image.files.first.bytes;
      String baseimage = base64Encode(imageBytes!);
      if (mounted) setState(() {});
      post = baseimage;
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => CreatePosts(post, user, caption, upvotes)));
    } else {
      print("File picker error");
    }
  }

:图像显示使用:

child: Container(
                                height:
                                    MediaQuery.of(context).size.height / 4.3,
                                width: MediaQuery.of(context).size.width / 3.4,
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(30),
                                    image: DecorationImage(
                                      fit: BoxFit.cover,
                                      image: Image.memory(
                                        _bytesImage,
                                        gaplessPlayback: true,
                                      ).image,
                                    ))),
                          ),

I was using ImagePicker in my application to select and upload images, but it recently started giving me errors, and constantly glitches when selecting png format images.

For this reason I switched to File picker. But it only works somewhat, and my application still gets stuck. I can only see its display, the image unfortunately does not get stored in the backend (jpg and jpeg images work fine).

Here is the image picker code (if there is a workaround uploading png images using this package, it would be much appreciated):

final ImagePicker _picker = ImagePicker();

  Future imageSelectorGallery() async {
    var image = (await _picker.pickImage(
      source: ImageSource.gallery,
    ));
    if (image != null) {
      Uint8List imageBytes = await image
          .readAsBytes(); // A fixed-length list of 8-bit unsigned integers which is the file read as bytes
      String baseimage = base64Encode(imageBytes);
      if (mounted) setState(() {});
      post = baseimage;
      Navigator.push(context,MaterialPageRoute(builder: (context) => CreatePosts(post,user,caption,upvotes)));
    }
  }

Here is the file picker code which I have implemented, any help figuring out the error here would also be appreciated:

Future imageSelectorGallery() async {
    FilePickerResult? image = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],
    );
    if (image != null) {
      Uint8List? imageBytes = image.files.first.bytes;
      String baseimage = base64Encode(imageBytes!);
      if (mounted) setState(() {});
      post = baseimage;
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => CreatePosts(post, user, caption, upvotes)));
    } else {
      print("File picker error");
    }
  }

The image is displayed using:

child: Container(
                                height:
                                    MediaQuery.of(context).size.height / 4.3,
                                width: MediaQuery.of(context).size.width / 3.4,
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(30),
                                    image: DecorationImage(
                                      fit: BoxFit.cover,
                                      image: Image.memory(
                                        _bytesImage,
                                        gaplessPlayback: true,
                                      ).image,
                                    ))),
                          ),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文