Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Folder When Installing Application

Tags:

flutter

How to create folder in device storage to save files?

This is the code to download file into device :

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)
   );
},

enter image description here

like image 694
Denis Ramdan Avatar asked Sep 10 '25 13:09

Denis Ramdan


2 Answers

You can create directory when app is launched. In the initState() method of your first screen do the logic.

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();
}

You need to import these libraries:

import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
like image 147
Raj Yadav Avatar answered Sep 13 '25 07:09

Raj Yadav


From what I saw is, you are not using appDocDir and appDocPath anywhere, cause you are saving files in /sdcard/myapp.

Please check if you are asking and granting the storage permission and also there is no way to store files in sdcard like you are doing. Either make use of predefined directories like (Document, Pictures etc.) or use device root directory that starts with storage/emulated/0

like image 28
CopsOnRoad Avatar answered Sep 13 '25 07:09

CopsOnRoad