如何在 Dart/Flutter 中读取本地图像文件的字节数? [英] How to read bytes of a local image file in Dart/Flutter?

查看:95
本文介绍了如何在 Dart/Flutter 中读取本地图像文件的字节数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个占位符图像文件,该文件已作为 "assets/placeholder.png" 添加到我的代码中,但我收到了 File not found 错误.这就是我在 dartlang 文档中的做法...

I would like to use a placeholder image file that I've added to my code as "assets/placeholder.png", but I'm getting a File not found error. This is how I'm doing it from the dartlang documentation...

var bytes = await new File('assets/placeholder.png').readAsBytes();
String base64 = CryptoUtils.bytesToBase64(bytes);

bytes 变量每次都会出错.如何保存本地保存的图像文件的字节数?

The bytes variable errors every time. How can I save the bytes of a locally saved image file?

推荐答案

使用 Flutter 环境,如果你想访问你的资产,你必须使用 AssetBundle (https://flutter.io/assets-and-images/).

With Flutter environment, you have to use AssetBundle if you want to access to your assets (https://flutter.io/assets-and-images/).

import 'package:flutter/services.dart' show rootBundle;


ByteData bytes = await rootBundle.load('assets/placeholder.png');

这篇关于如何在 Dart/Flutter 中读取本地图像文件的字节数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆