【发布时间】:2019-08-13 04:52:12
【问题描述】:
有没有办法将资产图像用作文件。 我需要一个文件,以便可以使用 http 在 Internet 上对其进行测试。我尝试了 Stackoverflow.com (How to load images with image.file) 的一些答案,但收到错误“无法打开文件,(操作系统错误:没有这样的文件或目录,errno = 2)”。附加的代码行也给出了错误。
File f = File('images/myImage.jpg');
RaisedButton(
onPressed: ()=> showDialog(
context: context,
builder: (_) => Container(child: Image.file(f),)),
child: Text('Show Image'),)
使用 Image.memory 小部件(作品)
Future<Null> myGetByte() async {
_byteData = await rootBundle.load('images/myImage.jpg');
}
/// called inside build function
Future<File> fileByte = myGetByte();
/// Show Image
Container(child: fileByte != null
? Image.memory(_byteData.buffer.asUint8List(_byteData.offsetInBytes, _
byteData.lengthInBytes))
: Text('No Image File'))),
【问题讨论】:
-
使用 AssetBundle 将其读入内存并存储到文件中。