安装应用程序时创建文件夹

2024-04-27

如何在设备存储中创建文件夹来保存文件?

这是将文件下载到设备的代码:

import 'package:flutter_downloader/flutter_downloader.dart';

onTap: () async { //ListTile attribute
   Directory appDocDir = await getApplicationDocumentsDirectory();                
   String appDocPath = appDocDir.path;
   final taskId = await FlutterDownloader.enqueue(
     url: 'http://myapp/${attach[index]}',
     savedDir: '/sdcard/myapp',
     showNotification: true, // show download progress in status bar (for Android)
     clickToOpenDownloadedFile: true, // click on notification to open downloaded file (for Android)
   );
},

您可以在应用程序启动时创建目录。 在里面initState()第一个屏幕的方法执行逻辑。

Ex.

createDir() async {
  Directory baseDir = await getExternalStorageDirectory(); //only for Android
  // Directory baseDir = await getApplicationDocumentsDirectory(); //works for both iOS and Android
  String dirToBeCreated = "<your_dir_name>";
  String finalDir = join(baseDir, dirToBeCreated);
  var dir = Directory(finalDir);
  bool dirExists = await dir.exists();
  if(!dirExists){
     dir.create(/*recursive=true*/); //pass recursive as true if directory is recursive
  }
  //Now you can use this directory for saving file, etc.
  //In case you are using external storage, make sure you have storage permissions.
}

@override
initState(){
  createDir(); //call your method here
  super.initState();
}

您需要导入这些库:

import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

安装应用程序时创建文件夹 的相关文章

随机推荐