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

3 Answers 3

21

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" />

THIS IS BY FAR THE BEST SOLUTION:
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.

NOTE II:
As of Android 11 and above, apps have been banned from creating custom folders using "storage/emulated/0/$folderName"

The solution below is OBSOLETE as of Android 11 and above:
Reference

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

Sign up to request clarification or add additional context in comments.

7 Comments

Not able to see created "$folderName" folder in phone when I connected to computer.
Yes, connected phone to computer via usb cable. Please suggest.
@Kamlesh the folder is created in Android>data>com.yourpackagename>files>your folder name.
Not working I used same code but still directory is not showing in phone
@ShubhamNarkhede getExternalStorageDirectory() creates in ``` Android>data>com.yourpackagename>files>your folder name```
|
9

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

}

2 Comments

this won't work unless the app has storage permission. Refer this answer which also includes permission request stackoverflow.com/a/64075888/13241651
im getting creation failed response when tried to create a folder
1

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.

1 Comment

thanks for thats but all of them not provide the path I need I test it i will see on the repo of the plugin if they are solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.