【问题标题】:Flutter - How to download video and save them to internal storage?Flutter - 如何下载视频并将其保存到内部存储?
【发布时间】:2020-06-30 20:55:26
【问题描述】:

我正在开发一个颤振项目,我需要从服务器功能实现视频下载。我正在考虑使用 Dio 库并将下载的视频保存到getApplicationDocumentsDirectory(),但我没有找到我的示例想要实现,我尝试了一些示例并尝试修改它们,但它没有奏效。 任何人都知道如何管理它?谢谢 。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码

    代码sn-p

    Future<void> downloadFile() async {
        Dio dio = Dio();
    
        try {
          var dir = await getApplicationDocumentsDirectory();
          print("path ${dir.path}");
          await dio.download(imgUrl, "${dir.path}/demo.mp4",
              onReceiveProgress: (rec, total) {
            print("Rec: $rec , Total: $total");
    
            setState(() {
              downloading = true;
              progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
            });
          });
        } catch (e) {
          print(e);
        }
    
        setState(() {
          downloading = false;
          progressString = "Completed";
        });
        print("Download completed");
      }
    

    工作演示

    输出

    I/flutter (18244): path /data/user/0/yourdoamin.yourproject/app_flutter
    I/flutter (18244): Rec: 2124 , Total: 2429896
    I/flutter (18244): Rec: 4883 , Total: 2429896
    ...
    I/flutter (18244): Rec: 2429896 , Total: 2429896
    I/flutter (18244): Download completed
    

    完整代码

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:dio/dio.dart';
    import 'package:path_provider/path_provider.dart';
    
    void main() => runApp(MaterialApp(
          home: MyApp(),
          debugShowCheckedModeBanner: false,
        ));
    
    class MyApp extends StatefulWidget {
      @override
      MyAppState createState() {
        return new MyAppState();
      }
    }
    
    class MyAppState extends State<MyApp> {
      final imgUrl = "https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";
      bool downloading = false;
      var progressString = "";
    
      @override
      void initState() {
        super.initState();
    
        downloadFile();
      }
    
      Future<void> downloadFile() async {
        Dio dio = Dio();
    
        try {
          var dir = await getApplicationDocumentsDirectory();
          print("path ${dir.path}");
          await dio.download(imgUrl, "${dir.path}/demo.mp4",
              onReceiveProgress: (rec, total) {
            print("Rec: $rec , Total: $total");
    
            setState(() {
              downloading = true;
              progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
            });
          });
        } catch (e) {
          print(e);
        }
    
        setState(() {
          downloading = false;
          progressString = "Completed";
        });
        print("Download completed");
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("AppBar"),
          ),
          body: Center(
            child: downloading
                ? Container(
                    height: 120.0,
                    width: 200.0,
                    child: Card(
                      color: Colors.black,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          CircularProgressIndicator(),
                          SizedBox(
                            height: 20.0,
                          ),
                          Text(
                            "Downloading File: $progressString",
                            style: TextStyle(
                              color: Colors.white,
                            ),
                          )
                        ],
                      ),
                    ),
                  )
                : Text("No Data"),
          ),
        );
      }
    }
    

    【讨论】:

    • 我花了很多时间寻找那个目录但找不到它。我正在下载视频。
    • 为了将文件下载到android中的下载文件夹,您必须请求首先在android清单中指定的存储权限,然后写入/storage/emulated/0/Download。 path provider提供的方法只提供apps本地目录的位置。
    • @chunhunghan 当网络连接下载失败时会发生什么,我们如何找到该文件稍后播放?
    猜你喜欢
    • 2015-12-23
    • 2020-10-10
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2021-04-16
    • 1970-01-01
    相关资源
    最近更新 更多