[英]downloads_path_provider — OS Error: Permission denied, errno = 13
I use downloads_path_provider
package to write to download directory.我使用downloads_path_provider
package 写入下载目录。 I set up it as it was recomended <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
to project AndroidManifest.xml
我按照推荐的方式设置它<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
来投影AndroidManifest.xml
But when I try to write I get this error 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)
但是当我尝试编写时,我收到此错误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)
here is the code这是代码
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');
}
How can I write to the downloads folder?如何写入下载文件夹?
AndroidManifest.xml 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"
...
here is how I get downloadsDirectory这是我获取下载目录的方式
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;
});
}
As mentioned in this answer, you will also need to ask your user explicitly for permission to use external storage,you can use this package for that如this answer中所述,您还需要明确询问您的用户是否允许使用外部存储,您可以使用此package
Update:更新:
this package turns to be better and has a better score,you can use it like this:这个package 变得更好并且有更好的分数,你可以这样使用它:
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.