我用downloads_path_provider包写下载目录。我将其设置为推荐 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />用于项目AndroidManifest.xml
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)
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'); }
如何写入下载文件夹?
<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; }); }
如该答案中所述,您还需要明确询问您的用户使用外部存储的权限,您可以将该软件包用于
更新:
这个软件包变得更好,并且得分更高,您可以像这样使用它:
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());