首页 > 解决方案 > 在您的 AndroidManifest.xml 文件中添加行 - 无法提供权限

问题描述

我已经授予允许外部存储文档并可能列出所有 PDF 文档的权限。尽管将这些行添加到您的 AndroidManifest.xml 文件中,但在启动小部件时仍会出现错误消息。

请告知如何修复此错误消息。

在此处输入图像描述

Exception has occurred.
FileManagerError (    

    Try to add thes lines to your AndroidManifest.xml file

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

    and grant storage permissions to your applicaion from app settings
    

FileSystemException: Directory listing failed, path = '/storage/emulated/0/' (OS Error: Permission denied, errno = 13))

这是错误来自的方法:


  void getFiles() async { //asyn function to get list of files
      List<StorageInfo> storageInfo = await PathProviderEx.getStorageInfo();
      var root = storageInfo[0].rootDir; //storageInfo[1] for SD card, geting the root directory
      var fm = FileManager(root: Directory(root)); //
      files = await fm.filesTree( 
        excludedPaths: ["/storage/emulated/0/Android"],
        extensions: ["pdf"] //optional, to filter files, list only pdf files
      );
      setState(() {}); //update the UI
  }

标签: androidfirebaseflutterdartpermissions

解决方案


添加以下权限后:

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

将以下包添加到pubspec.yaml文件中:

https://github.com/Baseflow/flutter-permission-handler

dependencies:
  permission_handler: ^5.0.1+1

然后你可以请求存储权限:

if (await Permission.storage.request().isGranted) {
  // Either the permission was already granted before or the user just granted it.
}

推荐阅读