Flutter 颤振-如何下载视频并将其保存到内部存储?
我在Flatter的一个项目中工作,我需要实现从服务器下载视频的功能。我正在考虑使用Dio库并将下载的视频保存到Flutter 颤振-如何下载视频并将其保存到内部存储?,flutter,Flutter,我在Flatter的一个项目中工作,我需要实现从服务器下载视频的功能。我正在考虑使用Dio库并将下载的视频保存到getApplicationDocumentsDirectory(),但我还没有找到我想要实现的示例,我尝试了一些例子,并试图修改它们,但它就是不起作用。 有人知道如何管理吗?谢谢 您可以在下面复制粘贴运行完整代码 代码片段 Future<void> downloadFile() async { Dio dio = Dio(); try { v
getApplicationDocumentsDirectory()
,但我还没有找到我想要实现的示例,我尝试了一些例子,并试图修改它们,但它就是不起作用。
有人知道如何管理吗?谢谢 您可以在下面复制粘贴运行完整代码
代码片段
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");
}
完整代码
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"),
),
);
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
进口“包装:dio/dio.dart”;
导入“package:path_provider/path_provider.dart”;
void main()=>runApp(MaterialApp(
主页:MyApp(),
debugShowCheckedModeBanner:false,
));
类MyApp扩展了StatefulWidget{
@凌驾
MyAppState createState(){
返回新的MyAppState();
}
}
类MyAppState扩展了状态{
最终imgUrl=”https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";
bool=false;
var progressString=“”;
@凌驾
void initState(){
super.initState();
下载文件();
}
Future downloadFile()异步{
Dio Dio=Dio();
试一试{
var dir=等待getApplicationDocumentsDirectory();
打印(“路径${dir.path}”);
等待dio.download(imgUrl,“${dir.path}/demo.mp4”,
收到进度:(记录,总计){
打印(“记录:$Rec,总计:$Total”);
设置状态(){
下载=真;
progressString=(记录/总计)*100.ToStringsFixed(0)+“%”;
});
});
}捕获(e){
印刷品(e);
}
设置状态(){
下载=假;
progressString=“已完成”;
});
打印(“下载完成”);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“AppBar”),
),
正文:中(
儿童:下载
?容器(
身高:120.0,
宽度:200.0,
孩子:卡片(
颜色:颜色,黑色,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
CircularProgressIndicator(),
大小盒子(
身高:20.0,
),
正文(
“正在下载文件:$progressString”,
样式:TextStyle(
颜色:颜色,白色,
),
)
],
),
),
)
:文本(“无数据”),
),
);
}
}
您可以复制粘贴运行下面的完整代码代码片段
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");
}
完整代码
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"),
),
);
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
进口“包装:dio/dio.dart”;
导入“package:path_provider/path_provider.dart”;
void main()=>runApp(MaterialApp(
主页:MyApp(),
debugShowCheckedModeBanner:false,
));
类MyApp扩展了StatefulWidget{
@凌驾
MyAppState createState(){
返回新的MyAppState();
}
}
类MyAppState扩展了状态{
最终imgUrl=”https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";
bool=false;
var progressString=“”;
@凌驾
void initState(){
super.initState();
下载文件();
}
Future downloadFile()异步{
Dio Dio=Dio();
试一试{
var dir=等待getApplicationDocumentsDirectory();
打印(“路径${dir.path}”);
等待dio.download(imgUrl,“${dir.path}/demo.mp4”,
收到进度:(记录,总计){
打印(“记录:$Rec,总计:$Total”);
设置状态(){
下载=真;
progressString=(记录/总计)*100.ToStringsFixed(0)+“%”;
});
});
}捕获(e){
印刷品(e);
}
设置状态(){
下载=假;
progressString=“已完成”;
});
打印(“下载完成”);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“AppBar”),
),
正文:中(
儿童:下载
?容器(
身高:120.0,
宽度:200.0,
孩子:卡片(
颜色:颜色,黑色,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
CircularProgressIndicator(),
大小盒子(
身高:20.0,
),
正文(
“正在下载文件:$progressString”,
样式:TextStyle(
颜色:颜色,白色,
),
)
],
),
),
)
:文本(“无数据”),
),
);
}
}
我花了很多时间查找该目录,但找不到它。我正在下载视频。要将文件下载到android中的下载文件夹,您必须先请求android清单中指定的存储权限,然后写入/storage/emulated/0/download。路径提供程序提供的方法只提供应用程序本地目录的位置。我花了很多时间查找该目录,但无法找到它。我正在下载视频。要将文件下载到android中的下载文件夹,您必须先请求android清单中指定的存储权限,然后写入/storage/emulated/0/download。路径提供程序提供的方法仅提供应用程序本地目录的位置。