如何在本地存储/外部Flutter中创建文件夹?

时间:2019-11-28 17:16:04

标签: flutter dart flutter-dependencies

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

这当然是我尝试的操作,我设置了写入存储的权限,但是此代码在此路径/storage/emulated/0/Android/data/com.example.test_app/files/some_name上创建了一个目录 而我需要的是在此路径/storage/emulated/0/some_name上创建任何关于我做错了什么的想法,或者它们是做那件事的另一种方式?

3 个答案:

答案 0 :(得分:2)

如果要在/storage/emulated/0中创建目录,请尝试此操作。

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

}

答案 1 :(得分:0)

根据插件代码,getExternalStorageDirectory()函数采用StorageDirectory类型的可选参数。您可以尝试提供type参数。 可用类型为:

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

有关其他详细信息,请参阅this code 如果问题仍然没有解决,您可以向插件作者提出请求。

希望有帮助。

答案 2 :(得分:0)

为我工作

permission_handler

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