Flutter 保存一张网络图片到本地目录 [英] Flutter save a network image to local directory
问题描述
在 Flutter 中如何将图像从网络保存到本地目录.
In Flutter how to save an image from network to the local directory.
我是编码和解码图像的新手.有人能指出我正确的方向吗?
I am new to encoding and decoding images. Can anyone point me in the right direction?
推荐答案
如果您只想将图像(例如:.png)保存到设备,您可以通过简单的 get (http/http.dart) 和一个文件 (dart:io).
If all you want is to save an image (example: a .png) to the device, you can easily achieve this with a simple get (http/http.dart) and a File (dart:io).
为此,您可以参考以下示例:
To do so, you can base yourself in the example below:
var response = await http.get(imgUrl);
Directory documentDirectory = await getApplicationDocumentsDirectory();
File file = new File(join(documentDirectory.path, 'imagetest.png'));
file.writeAsBytesSync(response.bodyBytes); // This is a sync operation on a real
// app you'd probably prefer to use writeAsByte and handle its Future
请注意,在上面的案例中,我使用了 dart pub 中的path_provider"包.总的来说,您至少会导入以下项目:
Note that in the case above I’ve used the ‘path_provider’ package from the dart pub. In overall you would have imported at least these items:
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
这篇关于Flutter 保存一张网络图片到本地目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!