简体   繁体   English

downloads_path_provider — 操作系统错误:权限被拒绝,errno = 13

[英]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 thatthis 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.

相关问题 错误:flutter 中的 downloads_path_provider - Error: downloads_path_provider in flutter Flutter 操作系统错误:权限被拒绝,errno = 13 - Flutter OS Error: Permission denied, errno = 13 FileSystemException:创建失败,路径 = '/storage/emulated/0/4k'(操作系统错误:权限被拒绝,errno = 13)Flutter - FileSystemException: Creation failed, path = '/storage/emulated/0/4k' (OS Error: Permission denied, errno = 13) Flutter 如何解决 FileSystemException: Cannot open file, path ="" (OS Error: Permission denied, errno = 13) - How to solve FileSystemException: Cannot open file, path ="" (OS Error: Permission denied, errno = 13) 如何摆脱:“FileSystemException:无法打开文件,路径 = '...'(操作系统错误:权限被拒绝,errno = 13)”? - How to get rid of: "FileSystemException: Cannot open file, path = '...' (OS Error: Permission denied, errno = 13)"? 无法打开文件,路径 = './pubspec.lock'(操作系统错误:权限被拒绝,errno = 13) - Cannot open file, path = './pubspec.lock' (OS Error: Permission denied, errno = 13) 如何解决操作系统错误:权限被拒绝,颤动中的 errno = 13 - How to solve OS Error: Permission denied, errno = 13 in flutter 无法创建数据报套接字(操作系统错误:权限被拒绝,errno = 13 - Failed to create datagram socket (OS Error: Permission denied, errno = 13 重新安装flutter应用程序时的FileSystemException(FileSystemException:无法打开文件,路径=&#39;&#39;(操作系统错误:权限被拒绝,errno = 13)) - FileSystemException (FileSystemException: Cannot open file, path = '' (OS Error: Permission denied, errno = 13)) when flutter app is re-installed Flutter - 将文件保存到下载文件夹 - downloads_path_provider - Flutter - save file to download folder - downloads_path_provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM