gpt4 book ai didi

flutter - 保存图像和视频以供离线访问

转载 作者:行者123 更新时间:2023-12-03 04:40:43 27 4
gpt4 key购买 nike

我正在开发一个从REST API中获取自定义对象的应用程序。该对象的类称为MyItem,它看起来像这样:

class MyItem {
String title;
String profilePicURL;
String videoURL;
}
如您所见,该类包含两个URL,分别指向 pngmp4文件。
我想实现一项功能,该功能允许用户下载对象,以便离线访问其内容。保存 title属性没有问题,但是如何保存两个URL(因为我不想保存URL本身,所以我想保存它指向的文件)。
知道在Flutter和Dart中这样做的最好方法是什么?
谢谢!

最佳答案

 import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
var directory = await getApplicationDocumentsDirectory();
Dio dio = Dio();




//Below function will download the file you want from url and save it locally
void Download(String title, String downloadurl) async{

try{
await dio.download(downloadurl,"${directory.path}/$title.extensionoffile",
onReceiveProgress: (rec,total){
print("Rec: $rec, Total:$total");
setState(() {
//just to save completion in percentage
String progressString = ((rec/total)*100).toStringAsFixed(0)+"%";

}
);
});
}
catch(e){

//Catch your error here
}
}
现在再说一次,只要您想使用
var directory = await getApplicationDocumentsDirectory();
String filepath = "{directory.path}/filename.fileextension";
现在,您可以使用此 Image.file('filepath'); //显示这些图像
你也可以用
video player plugin
再次 VideoPlayerController.file('filepath') //显示视频但正确阅读文档
这些只是一个完整的步骤或一个更广阔的 View ,您需要将它们用作映射并构建代码,即已保存了正确的文件名和扩展名或已正确提取或映射了它们

关于flutter - 保存图像和视频以供离线访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63305451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com