最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Flutter Error while saving to the Download folder (OS Error: Permission denied, errno = 13) - Stack Overflow

programmeradmin3浏览0评论

When I try to store a file in the main download directory (not the \Android\data\com.example.myApp one) it throws an exception.

PathAccessException: Cannot open file, path = '/storage/emulated/0/Download/MyFile.pdf' (OS Error: Permission denied, errno = 13)

Theres also this message from the Permission-Handler package (which is on version 11.4.0 atm).

D/permissions_handler(21802): No permissions found in manifest for: []22

My Android Manifest:

<manifest xmlns:android=";>
  <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <application android:label="@string/app_name" android:name="${applicationName}" android:icon="@mipmap/ic_launcher" android:requestLegacyExternalStorage="true">
    ...
  </application>
</manifest>

My App code:

Future<void> downLoadFile(String fileName) async {
  //... getting the response from an API
  final bytes = response.bodyBytes;
  
  var status = await Permission.storage.status;
  if (!status.isGranted) {
    await Permission.storage.request();
    if(!(await Permission.storage.status).isGranted) return;
  }
  status = await Permission.manageExternalStorage.status;
  if (!status.isGranted) {
    await Permission.manageExternalStorage.request();
  }

  String? downloadPath = await getDownloadPath();
  if(downloadPath == null) throw Exception("Failed get the download path");
  var filePath = "$downloadPath/$fileName";
  final bytes = result;
  File(filePath).writeAsBytes(bytes);
}


Future<String?> getDownloadPath() async {
  Directory? directory;
  try {
    if (Platform.isIOS) {
      directory = await getApplicationDocumentsDirectory();
    } else {
      directory = Directory("/storage/emulated/0/Download");
      if (!await directory.exists()) directory = await getExternalStorageDirectory();
    }
  } catch (err) {
    print("Cannot get download folder path");
  }
  return directory?.path;
}

I already tried most of the solutions under this Post

When I try to store a file in the main download directory (not the \Android\data\com.example.myApp one) it throws an exception.

PathAccessException: Cannot open file, path = '/storage/emulated/0/Download/MyFile.pdf' (OS Error: Permission denied, errno = 13)

Theres also this message from the Permission-Handler package (which is on version 11.4.0 atm).

D/permissions_handler(21802): No permissions found in manifest for: []22

My Android Manifest:

<manifest xmlns:android="http://schemas.android/apk/res/android">
  <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <application android:label="@string/app_name" android:name="${applicationName}" android:icon="@mipmap/ic_launcher" android:requestLegacyExternalStorage="true">
    ...
  </application>
</manifest>

My App code:

Future<void> downLoadFile(String fileName) async {
  //... getting the response from an API
  final bytes = response.bodyBytes;
  
  var status = await Permission.storage.status;
  if (!status.isGranted) {
    await Permission.storage.request();
    if(!(await Permission.storage.status).isGranted) return;
  }
  status = await Permission.manageExternalStorage.status;
  if (!status.isGranted) {
    await Permission.manageExternalStorage.request();
  }

  String? downloadPath = await getDownloadPath();
  if(downloadPath == null) throw Exception("Failed get the download path");
  var filePath = "$downloadPath/$fileName";
  final bytes = result;
  File(filePath).writeAsBytes(bytes);
}


Future<String?> getDownloadPath() async {
  Directory? directory;
  try {
    if (Platform.isIOS) {
      directory = await getApplicationDocumentsDirectory();
    } else {
      directory = Directory("/storage/emulated/0/Download");
      if (!await directory.exists()) directory = await getExternalStorageDirectory();
    }
  } catch (err) {
    print("Cannot get download folder path");
  }
  return directory?.path;
}

I already tried most of the solutions under this Post

Share Improve this question edited Apr 2 at 7:33 deceze 523k88 gold badges800 silver badges943 bronze badges asked Mar 24 at 15:10 ShoginShogin 13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

in Manifest file

<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>

and permission for

await Permission.accessMediaLocation.request();

I ended up using the MediaStore API, and that works now.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论