如何在本地存储/外部 Flutter 中创建文件夹?
Posted
技术标签:
【中文标题】如何在本地存储/外部 Flutter 中创建文件夹?【英文标题】:How To Create Folder in Local Storage/External Flutter? 【发布时间】:2020-03-24 09:34:06 【问题描述】: 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
任何关于我做错了什么的想法,或者他们是另一种方式来做到这一点??
【问题讨论】:
【参考方案1】:如果你想在/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();
【讨论】:
除非应用程序具有存储权限,否则这将不起作用。请参阅此答案,其中还包括权限请求***.com/a/64075888/13241651【参考方案2】:这对我有用permission_handler
但首先要在您的 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;
ios 和 Android 的 OR(最佳)
Future<String> createFolder(String cow) async
final dir = Directory((await getExternalStorageDirectory())!.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
文件,其他应用将无法扫描您的文件夹。
【讨论】:
连接电脑时看不到手机中创建的“$folderName”文件夹。 是的,通过 USB 数据线将手机连接到计算机。请提出建议。 @Kamlesh 该文件夹在Android>data>com.yourpackagename>files>your folder name
中创建。【参考方案3】:
根据插件代码,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 如果您的问题仍未解决,您可以向插件作者提出请求。
希望对你有帮助。
【讨论】:
谢谢你,但他们都没有提供我需要的路径我测试它如果他们是解决方案,我将在插件的回购中看到以上是关于如何在本地存储/外部 Flutter 中创建文件夹?的主要内容,如果未能解决你的问题,请参考以下文章
使用存储在 s3 中的 parquet 文件在 Greenplum 中创建外部表
在 Android Q 的外部存储中创建 App-Specific 文件夹
使用镶木地板文件作为存储在 Hive 中创建外部表后获取 NULL
我们不能以编程方式在 Android 'R' 的外部存储中创建文件夹,即使用颤振的 android 11(操作系统错误:不允许操作,errno = 1)