首页 > 解决方案 > 如果我将文件放在 getApplicationDocumentsDirectory() 目录中,文件将存储在哪里?它会增加应用程序的大小吗?

问题描述

Future<void> downloadFile() async{
    Dio dio = Dio() ;
    try{
      var dir = await getApplicationDocumentsDirectory();
      print('DIR is :${dir}');
      await dio.download(pdfUrl, "${dir.path}/sample.pdf",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");
  }

我想存储我下载的 pdf 以供离线使用,我该如何实现呢?如何保存在 downloads/(sample.pdf) 本地文件夹中,并且可以在离线时检索文件?

标签: flutterflutter-dependencies

解决方案


在 iOS 上,这使用 NSDocumentDirectory API。如果数据不是用户生成的,请考虑改用 getApplicationSupportDirectory。

在 Android 上,这在上下文中使用 getDataDirectory API。如果数据旨在对用户可见,请考虑使用 getExternalStorageDirectory。

所以我不知道App size是什么意思?如果您将文件下载到应用程序的目录,这将明显增加应用程序缓存或其他大小,但不会增加 apk 大小。


推荐阅读