我是 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)
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)
Hưn*_*ịnh 16
我有同样的问题
我的解决:
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)
有关更多信息,请尝试此链接
小智 6
除了 Rajil TL 的回答还包括
targetSdkVersion 28
Run Code Online (Sandbox Code Playgroud)
并停止进程并运行应用程序。热重载不足以反映 build.gradle 的更改。