saveNetworkImage static method

Future<String> saveNetworkImage(
  1. String imageUrl
)

保存网络图片到本地

Implementation

static Future<String> saveNetworkImage(String imageUrl) async {
  try {
    // 创建 Dio 实例
    final dio = Dio();

    // 下载网络图片
    final response = await dio.get(imageUrl,
        options: Options(responseType: ResponseType.bytes));
    final bytes = response.data as Uint8List;

    // 获取临时目录路径
    final tempDir = await getTemporaryDirectory();
    final file = File('${tempDir.path}/downloaded_image.jpg');

    // 将图片数据写入文件
    await file.writeAsBytes(bytes);

    return file.path;
  } catch (e) {
    // 处理异常情况
    debugPrint('Error saving network image: $e');
    return '';
  }
}