Fluttter android(操作系统错误:Permission denied,errno = 13)write to Downloads folder

kiayqfof  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(215)

我尝试将设备临时文件夹中的文件复制到Downloads文件夹,但在file.copy函数中出现此错误:

  1. PathAccessException: Cannot copy file to '/storage/emulated/0/Download/test.pdf', path = '/data/user/0/myapp/cache/file-pdf.pdf' (OS Error: Permission denied, errno = 13)

字符串
代码如下:

  1. await Permission.storage.request();
  2. Directory downloadFolder = Directory("/storage/emulated/0/Download/")
  3. String pdfName = "test";
  4. await file.copy("${downloadFolder.path}$pdfName.pdf");


我正在使用permission_handler: ^11.1.0,我在AndroidManifest.xml上写了以下内容:

  1. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


以下是我的build.gradle配置:

  1. android {
  2. compileSdk flutter.compileSdkVersion
  3. ndkVersion flutter.ndkVersion
  4. defaultConfig {
  5. minSdkVersion 21
  6. targetSdkVersion flutter.targetSdkVersion
  7. }
  8. }


Flutter doctor输出:

  1. [✓] Flutter (Channel beta, 3.18.0-0.2.pre, on macOS 14.2.1 23C71 darwin-arm64, locale pt-BR)
  2. [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
  3. [✓] Xcode - develop for iOS and macOS (Xcode 15.1)
  4. [✓] Chrome - develop for the web
  5. [✓] Android Studio (version 2022.3)
  6. [✓] VS Code (version 1.85.1)
  7. [✓] Connected device (2 available)
  8. [✓] Network resources


我需要这个代码运行在Android 10到13.

eyh26e7m

eyh26e7m1#

当我将下面的标记添加到AndroidManifest.xml文件时,将文件保存在Downloads文件夹中工作(在Android 10到13上):

  1. <application
  2. ...
  3. android:requestLegacyExternalStorage="true">

字符串
感谢@blackapps的帮助。

相关问题