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

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

问题描述

我想使用一个已添加到代码中的占位符图像文件作为"assets/placeholder.png",但出现找不到文件"错误.这就是我在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(

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天全站免登陆