Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Create Folder in Local Storage/External Flutter?

   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 ??

like image 459
elmissouri16 Avatar asked Nov 28 '19 17:11

elmissouri16


2 Answers

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.

like image 140
Ray Zion Avatar answered Sep 27 '22 02:09

Ray Zion


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

}

like image 32
amir khan Avatar answered Sep 23 '22 02:09

amir khan