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

ep6jt1vc  于 2023-03-31  发布在  Flutter
关注(0)|答案(3)|浏览(212)
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任何想法是什么我做错了,或者他们是另一种方式来做到这一点?

dgiusagp

dgiusagp1#

对我来说这很有效

permission_handler
但首先要在Android Manifest中设置权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

这是迄今为止最好的解决方案:
适用于iOS和Android(最佳)

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

如果您将文件夹命名为createFolder(".folder"),该文件夹将被隐藏。
如果您在文件夹中创建.nomedia文件,其他应用将无法扫描您的文件夹。

注二

截至Android 11及以上版本,应用已被禁止使用“storage/emulated/0/$folderName”创建自定义文件夹
以下解决方案在Android 11及以上版本中已过时:
参考文献

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

cwdobuhd2#

如果您想在/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();
}

}

kdfy810k

kdfy810k3#

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

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如果您的问题仍未解决,您可以向插件作者提出请求。
希望有帮助。

相关问题