gpt4 book ai didi

dart - Flutter 将网络图片保存到本地目录

转载 作者:IT王子 更新时间:2023-10-29 06:35:13 27 4
gpt4 key购买 nike

在Flutter中如何将网络中的图片保存到本地目录。

我是编码和解码图像的新手。谁能指出我正确的方向?

最佳答案

如果您只想将图像(例如:.png)保存到设备,您可以通过简单的获取 (http/http.dart) 和文件 (dart:io) 轻松实现。

为此,您可以引用以下示例:

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”包。总的来说,您至少会导入这些项目:

import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

关于dart - Flutter 将网络图片保存到本地目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49455079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com