如何解决操作系统错误:权限被拒绝,颤动中的 errno = 13

qin*_*ing 13 flutter

我是 Flutter 的新手。我想将我的文档文件存储'/storage/emulated/0/Download/'。我有错误Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Download/file.pdf' (OS Error: Permission denied, errno = 13)

这是我的代码:

void download() async {
http.Response response = await http.post(url, headers: {"Accept": "application/json", HttpHeaders.authorizationHeader: 'Bearer}});

  File file = new File('/storage/emulated/0/Download/file.pdf');
  await file.writeAsBytes(response.bodyBytes);
}
Run Code Online (Sandbox Code Playgroud)

小智 46

在 Android Q 中,您需要在 AndroidManifest 文件中添加以下几行:

 <application
      android:requestLegacyExternalStorage="true"
Run Code Online (Sandbox Code Playgroud)

  • 与targetSdkVersion 29的变化有关吗? (2认同)
  • 是的,有道理。如果你的 targetSdkVersion 是 28,则不需要这一行。如果游览 `targetSdkVersion` 是 29 或更高,则需要此行 (2认同)
  • 添加此后,如果您收到“错误:属性 android:requestLegacyExternalStorage 未找到”,请检查[此答案](/sf/answers/4130269741/)。具体来说,请确保 android/app/build.gradle 中有 `compileSdkVersion = 29` (2认同)

Cha*_*nka 17

如果您使用的是 android 10/Q,只需将以下行添加到您的 android 清单中

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
   <application
        android:requestLegacyExternalStorage="true"
    >
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用...谢谢。我把 `android:requestLegacyExternalStorage="true"` 现在工作正常 (2认同)

Hưn*_*ịnh 16

我有同样的问题

我的解决:

使用permission_handler

import 'package:permission_handler/permission_handler.dart';
Run Code Online (Sandbox Code Playgroud)

在下载操作之前,您需要检查存储权限:

var status = await Permission.storage.status;
                  if (!status.isGranted) {
                    await Permission.storage.request();
                  }
Run Code Online (Sandbox Code Playgroud)

希望对身体有所帮助


A R*_*A R 12

确保您AndroidManifest file像这样定义了权限:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.yyy">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...
</manifest>
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请尝试链接

  • 谢谢您的回复,我已经授予了这些权限,但仍然有错误 (2认同)

小智 6

除了 Rajil TL 的回答还包括

targetSdkVersion 28
Run Code Online (Sandbox Code Playgroud)

并停止进程并运行应用程序。热重载不足以反映 build.gradle 的更改。