import 'package:path_provider/path_provider.dart';
import 'dart:io';
void createAppFolder() async {
final directory = await getExternalStorageDirectory();
final dirPath = '${directory.path}/some_name' ;
await new Directory(dirPath).create();
}
this whats I tried of course I set up the permission for writing to storage but this code creates a directory on this path /storage/emulated/0/Android/data/com.example.test_app/files/some_name
and whats I need is to be created on this path /storage/emulated/0/some_name
any idea of whats im doing wrong or they are another way to do thats ??
FOR ME THIS WORKS
permission_handler
But first set the permissions right in your Android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Future<String> createFolder(String cow) async {
final folderName = cow;
final path = Directory("storage/emulated/0/$folderName");
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
if ((await path.exists())) {
return path.path;
} else {
path.create();
return path.path;
}
}
OR for both IOS and Android (BEST)
Future<String> createFolder(String cow) async {
final dir = Directory((Platform.isAndroid
? await getExternalStorageDirectory() //FOR ANDROID
: await getApplicationSupportDirectory() //FOR IOS
)!
.path + '/$cow');
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
if ((await dir.exists())) {
return dir.path;
} else {
dir.create();
return dir.path;
}
}
NOTE:
If you name your createFolder(".folder")
that folder will be hidden.
If you create a .nomedia
file in your folder, other apps won't be able to scan your folder.
if you want to create dir in /storage/emulated/0
try this.
import 'dart:io';
_createFolder()async{
final folderName="some_name";
final path= Directory("storage/emulated/0/$folderName");
if ((await path.exists())){
// TODO:
print("exist");
}else{
// TODO:
print("not exist");
path.create();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With