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;
  }
相关推荐
2501_919749036 小时前
flutter鸿蒙:实现类似B站或抖音的弹幕功能
flutter·华为·harmonyos
用户0910 小时前
Flutter插件与包的本质差异
android·flutter·面试
浅蓝色11 小时前
flutter平台判断,这次应该没问题了。支持鸿蒙,插件已发布
flutter·harmonyos
怀君11 小时前
Flutter——打印之PdfPreview功能详细教程
flutter
唔661 天前
flutter实现web端实现效果
前端·flutter
sunly_1 天前
Flutter:启动动画Lottie
flutter
柿蒂1 天前
聊聊SliverPersistentHeader优先消费滑动的设计
android·flutter
fishofeyes1 天前
Riverpod使用
flutter
消失的旧时光-19432 天前
iFlutter --> Flutter 开发者 的 IntelliJ IDEA / Android Studio 插件
flutter·android studio·intellij-idea
马拉萨的春天2 天前
block的样式有哪些?如果copy的话分别会有啥样式
flutter·性能优化·ios的block