flutter 网络地址URL转file

方法1

c 复制代码
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

Future<File> _fileFromImageUrl() async {
    final response = await http.get(Uri.parse('https://example.com/xyz.jpg)');

    final documentDirectory = await getApplicationDocumentsDirectory();

    final file = File(join(documentDirectory.path, 'imagetest.png'));

    file.writeAsBytesSync(response.bodyBytes);

    return file;
  }

方法2

c 复制代码
import 'dart:io';   
import 'package:dio/dio.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';


Future<File> getImage({required String url}) async {
  /// Get Image from server
  final Response res = await Dio().get<List<int>>(
    url,
    options: Options(
        responseType: ResponseType.bytes,
      ),
    );

    /// Get App local storage
    final Directory appDir = await getApplicationDocumentsDirectory();

    /// Generate Image Name
    final String imageName = url.split('/').last;

    /// Create Empty File in app dir & fill with new image
    final File file = File(join(appDir.path, imageName));

    file.writeAsBytesSync(res.data as List<int>);

    return file;
}

方法3

c 复制代码
Future<File> fileFromImageUrl() async {
String img='https://pps.whatsapp.net/v/t61.24694-24/160266256_1115367465775510_3546030680878251116_n.jpg?ccb=11-4&oh=01_AdSsrMGOPfs8CUJsEkYImMUu5L4DAzt2ym8eBrdsMG5O0Q&oe=63D7B45E';
    final response = await http.get(Uri.parse(img));

    final documentDirectory = await getApplicationDocumentsDirectory();

    final file =
        File(p.join(documentDirectory.path, 'File Name.jpg'));

    file.writeAsBytesSync(response.bodyBytes);

    return file;
  }
相关推荐
农夫三拳_有点甜22 分钟前
Flutter MaterialApp 组件属性第一章
flutter
阿笑带你学前端26 分钟前
Flutter应用架构设计:基于Riverpod的状态管理最佳实践
前端·flutter
Zender Han12 小时前
Flutter 视频播放器——flick_video_player 介绍与使用
android·flutter·ios·音视频
恋猫de小郭17 小时前
Flutter Riverpod 3.0 发布,大规模重构下的全新状态管理框架
android·前端·flutter
RaidenLiu19 小时前
Riverpod 3:重建控制的实践与性能优化指南
前端·flutter
蒋星熠1 天前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
卢叁2 天前
Flutter之自定义TabIndicator
前端·flutter
萧雾宇2 天前
Android Compose打造仿现实逼真的烟花特效
android·flutter·kotlin
拜无忧2 天前
【教程】flutter常用知识点总结-针对小白
android·flutter·android studio
拜无忧2 天前
【教程】Flutter 高性能项目架构创建指南:从入门到高性能架构
android·flutter·android studio