gpt4 book ai didi

flutter - downloads_path_provider — 操作系统错误 : Permission denied, errno = 13

转载 作者:行者123 更新时间:2023-12-02 19:32:40 25 4
gpt4 key购买 nike

我使用 downloads_path_provider要写入下载目录的包。我按照推荐的方式设置了它 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />到项目AndroidManifest.xml

但是当我尝试写的时候我得到了这个错误 E/flutter (26091): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Download/secure-strorage-backup.txt' (OS Error: Permission denied, errno = 13)

这是代码

  final file = _localFile;
file.writeAsString('1234 5678\n');
file.writeAsString('1234 5678\n');

File get _localFile {
final path = _downloadsDirectory.path;
return File('$path/secure-strorage-backup.txt');
}

如何写入下载文件夹?

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.user.myappname">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name="io.flutter.app.FlutterApplication"
android:label="Secure Storage"
...

这是我获取下载目录的方式

  void initState() {
super.initState();
initDownloadsDirectoryState();
}

Future<void> initDownloadsDirectoryState() async {
Directory downloadsDirectory;
try {
downloadsDirectory = await DownloadsPathProvider.downloadsDirectory;
} on PlatformException {
print('Could not get the downloads directory');
}
if (!mounted) return;
setState(() {
_downloadsDirectory = downloadsDirectory;
});
}

最佳答案

this 中所述回答,你还需要明确询问你的用户是否允许使用外部存储,你可以使用this那个包

更新:

this包变得更好并且有更好的分数,你可以这样使用它:

                  await Permission.storage.request();
Directory downloadsDirectory = await DownloadsPathProvider.downloadsDirectory;
final String path = downloadsDirectory.path;
final File file = File('$path/secure-strorage-backup.txt');
await file.writeAsString('1234 5678\n');
await file.writeAsString('1234 5678\n');
print(await file.readAsString());

关于flutter - downloads_path_provider — 操作系统错误 : Permission denied, errno = 13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61559351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com