如何将资产中的图像复制到Flutter中的应用程序文档目录? [英] How to copy images in assets to application document directory in Flutter?

查看:60
本文介绍了如何将资产中的图像复制到Flutter中的应用程序文档目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的flutter应用程序具有以下资产.

My flutter app has the following assets.

assets:
 - "images/01.png"
 - "images/02.png"
 - "images/03.png"
 ...

我想将这些图像文件复制到我可以通过以下方式获得的本地路径.

I'd like to copy these image files to the local path I can get by the following.

 Directory docDir = await getApplicattionDocumentsDirectory();
 String localPath = docDir.path;

推荐答案

在应用程序目录中创建一个文件,并将您的资产图片逐字节复制到该文件中.就是这样!您复制了文件.

Create a file in your application directory and copy your asset image byte by byte to that file. That's it! You copied your file.

final Directory docDir = await getApplicationDocumentsDirectory();
final String localPath = docDir.path;
File file = File('$localPath/${path.split('/').last}');
final imageBytes = await rootBundle.load(path);
final buffer = imageBytes.buffer;
await file.writeAsBytes(
    buffer.asUint8List(imageBytes.offsetInBytes, imageBytes.lengthInBytes));

这篇关于如何将资产中的图像复制到Flutter中的应用程序文档目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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