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



Solution 1:[1]

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

}

Solution 2:[2]

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.

Solution 3:[3]

As per the plugin code, getExternalStorageDirectory() function takes optional parameter of StorageDirectory type. You can try providing the type argument. Available types are:

enum StorageDirectory {
  /// Contains audio files that should be treated as music.
  ///
  /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_MUSIC.
  music,

  /// Contains audio files that should be treated as podcasts.
  ///
  /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_PODCASTS.
  podcasts,

  /// Contains audio files that should be treated as ringtones.
  ///
  /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_RINGTONES.
  ringtones,

  /// Contains audio files that should be treated as alarm sounds.
  ///
  /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_ALARMS.
  alarms,

  /// Contains audio files that should be treated as notification sounds.
  ///
  /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_NOTIFICATIONS.
  notifications,

  /// Contains images. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_PICTURES.
  pictures,

  /// Contains movies. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_MOVIES.
  movies,

  /// Contains files of any type that have been downloaded by the user.
  ///
  /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DOWNLOADS.
  downloads,

  /// Used to hold both pictures and videos when the device filesystem is
  /// treated like a camera's.
  ///
  /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DCIM.
  dcim,

  /// Holds user-created documents. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DOCUMENTS.
  documents,
}

For additional details you can refer to this code If your issue still not solved you can raise a request to plugin author.

Hope it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 amir khan
Solution 2
Solution 3 Ganapat