教育营销型的网站建设,WordPress重力表单注册,营销推广公司经营范围,查企企官方网站看了下很多文章#xff0c;本地文件存储都只有存储txt文件#xff0c;我们探索下存储二进制文件吧。 
保存二进制文件到设备硬盘上。 我们保存一个图片到手机本地上#xff0c;并读取展示图片到app上。 以百度logo图为例子 
写入图片 逻辑如下#xff1a; 获取本地路径 -本地文件存储都只有存储txt文件我们探索下存储二进制文件吧。 
保存二进制文件到设备硬盘上。 我们保存一个图片到手机本地上并读取展示图片到app上。 以百度logo图为例子 
写入图片 逻辑如下 获取本地路径 - 根据图片url开始下载获取到二进制文件 - 图片文件写入到本地路径 写入本地文件 写入文件有几种方式点我查看file文档 分别是字符串和二进制而写入的时候分同步和异步所以写入的有4种。\ 
看writeAsBytes这个函数名字这厮需要bytes 代码 writeFile(String filePath, String imgUrl){File file  new File(filePath);if(!file.existsSync()) {file.createSync();}getRemoteFileStream(imgUrl, (fileInfo){if(fileInfo[ok]){file.writeAsBytes(fileInfo[bytes]);}else{debugPrint(异常);}});}获取远程文件流 新建一个http连接使用dartimport ‘dart:io’;自带的哦。 使用Uri.parse解析完整的url request.close()开始发送请求(小声bb这个命名一言难尽) 获取到数据后response是一个HttpClientResponse类型而HttpClientResponse类型Stream的实现 java复制代码abstract class HttpClientResponse implements StreamList 关于Stream说起来比较复杂推荐一个文章传送门 Stream最后会吐出List也就是我们需要的Bytes 代码 scss复制代码 
// get文件流
getRemoteFileStream(String url, cb) async{return new Future(() async{HttpClient httpClient  new HttpClient();HttpClientRequest request  await httpClient.getUrl(Uri.parse(url));HttpClientResponse response  await request.close();if (response.statusCode  HttpStatus.ok) {Listint buffer  [];response.listen((data){data.forEach((e){buffer.add(e);});},onDone: (){cb({ok: true,bytes: buffer});},onError: (e){print(e);});}else{debugPrint(HTTP request failed);}});
}写好了上面两个函数我们现在可以获取本地文件路径来写入图片了 最后一步执行写入 获取本地路径分为临时文件和文档路径 我们使用path_provider来获取本地路径 arduino复制代码path_provider: ^1.4.0 
import package:path_provider/path_provider.dart;
获取本地路径执行读写本地文件
代码
ini复制代码getLocalhostBooks() async{String imgUrl  https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/1/16e24dc995c95343~tplv-t2oaga2asx-image.image;Directory tempDir  await getTemporaryDirectory();Directory appDocDir  await getApplicationDocumentsDirectory();debugPrint(本地路径);debugPrint(tempDir.path);debugPrint(appDocDir.path);String testPath  ${appDocDir.path}/test.png;// // 写入图片// writeFile(testPath, imgUrl);// 读取图片readFile(testPath, imgUrl);
}读取 读取本地文件 ini复制代码 
readFile(String filePath, String imgUrl) async{ File file  new File(filePath); Uint8List fileBytes  await file.readAsBytes(); setState(() { imgBytes  fileBytes; print(imgBytes); }); } 
读取比较简单一点获取bytes然后渲染就是了。 ini复制代码setState(() { imgBytes  fileBytes; print(imgBytes); }); php复制代码Container( child: Image.memory(imgBytes, gaplessPlayback: true,), ) 效果 
链接https://juejin.cn/post/6844903985132879885 https://www.kkview.com 文章转载自: http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.jiuyungps.com.gov.cn.jiuyungps.com http://www.morning.zsgbt.cn.gov.cn.zsgbt.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.rsszk.cn.gov.cn.rsszk.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.dtpqw.cn.gov.cn.dtpqw.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.pbtdr.cn.gov.cn.pbtdr.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.ahlart.com.gov.cn.ahlart.com http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.rrgm.cn.gov.cn.rrgm.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.drndl.cn.gov.cn.drndl.cn http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.fjgwg.cn.gov.cn.fjgwg.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.rfrx.cn.gov.cn.rfrx.cn http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn http://www.morning.yrddl.cn.gov.cn.yrddl.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.yysqz.cn.gov.cn.yysqz.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.pwqyd.cn.gov.cn.pwqyd.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.gzgwn.cn.gov.cn.gzgwn.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.dxtxk.cn.gov.cn.dxtxk.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.qyrnp.cn.gov.cn.qyrnp.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.lynb.cn.gov.cn.lynb.cn