downloads_path_provider —操作系统错误:权限被拒绝,错误号= 13 [英] downloads_path_provider — OS Error: Permission denied, errno = 13

查看:161
本文介绍了downloads_path_provider —操作系统错误:权限被拒绝,错误号= 13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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"
        ...

这是我如何获得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;
    });
  }

解决方案

答案中所述,您还将需要明确询问您的用户使用外部存储的权限,您可以为此使用包 >

更新:

包变得更好,并且得分更高,您可以像这样使用它:

                  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());

I use downloads_path_provider package to write to download directory. I set up it as it was recomended <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> to project 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)

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

<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

Update:

this package turns to be better and has a better score,you can use it like 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());

这篇关于downloads_path_provider —操作系统错误:权限被拒绝,错误号= 13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆