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;
  }
相关推荐
vvilkim2 小时前
Flutter JSON解析全攻略:使用json_serializable实现高效序列化
flutter·json
LinXunFeng3 小时前
Flutter - GetX Helper 如何应用于旧页面
前端·flutter·开源
技术蔡蔡4 小时前
从Google IO学习Flutter
flutter·google·google io
vvilkim11 小时前
Flutter 状态管理基础:深入理解 setState 和 InheritedWidget
前端·javascript·flutter
97650333511 小时前
iOS 审核 cocos 4.3a【苹果机审的“分层阈值”设计】
flutter·游戏·unity·ios
程序员老刘·11 小时前
iOS 26 beta1 真机无法执行hot reload
flutter·ios·跨平台开发·客户端开发
ZFJ_张福杰13 小时前
【Flutter】性能优化总结
flutter·性能优化
BAGAE14 小时前
使用 Flutter 在 Windows 平台开发 Android 应用
android·大数据·数据结构·windows·python·flutter
肥肥呀呀呀1 天前
flutter 的lottie执行一次动画后关闭
开发语言·flutter
只可远观1 天前
Flutter Android打包和发布Build APK
前端·flutter·dart