简体   繁体   English

Flutter Android 11 权限如 WhatsApp

[英]Flutter Android 11 permissions like WhatsApp

I'm trying to build a Flutter application that runs on Android 11 and downloads files.我正在尝试构建一个在 Android 11 上运行并下载文件的 Flutter 应用程序。 I used to manage external storage permission to achieve this, but when the application asks for permission it goes to settings directly instead of asking for allow or deny within the app.我曾经通过管理外部存储权限来实现这一点,但是当应用程序请求权限时,它会直接进入设置,而不是在应用程序内请求允许或拒绝。

For example, WhatsApp stores data in the android/media folder, but it asks for permission directly within the application instead of going to the settings page.例如,WhatsApp 将数据存储在android/media文件夹中,但它直接在应用程序内请求权限,而不是转到设置页面。 Please refer the below images:请参考以下图片:

My application goes to settings like this / I need something like this我的应用程序进入这样的设置/我需要这样的东西

My permission handling code我的权限处理代码

Future<bool> requestPermission() async {
  var androidInfo = await DeviceInfoPlugin().androidInfo;
  var release = int.parse(androidInfo.version.release);
  Permission permission;
  if (release < 11) {
    permission = Permission.storage;
  } else {
    permission = Permission.manageExternalStorage;
  }
  if (await permission.isGranted) {
    return true;
  } else {
    var result = await permission.request();
    if (result == PermissionStatus.granted) {
      return true;
    } else {
      return false;
    }
  }
}

These are two different approaches to manage the access the external storage in the Android device for Android 11 and above.这是管理Android 11及更高版本的 Android 设备中外部存储访问的两种不同方法。

Files Go is trying to access the all files access permission .文件 Go 正在尝试访问all files access permission This gives app to access any read, write, access and share all files present in user storage(external or internal).这使应用程序可以访问任何读取、写入、访问和共享用户存储(外部或内部)中存在的所有文件。 You can read more about all files access permission from here - Manage all files on a storage device您可以从此处阅读有关所有文件访问权限的更多信息 - 管理存储设备上的所有文件

WhatsApp - instead of asking for all files access permission , is saving its media in files directory. WhatsApp - 不是要求all files access permission ,而是将其媒体保存在文件目录中。 Saving in the files or cache directory doesn't need access to all flies and a simple permission dialog to write and read storage does the task.保存在文件或缓存目录中不需要访问所有苍蝇,一个简单的权限对话框来写入和读取存储就可以完成任务。

You can read more about it in getExternalFilesDir .您可以在getExternalFilesDir中阅读更多相关信息。

Note: This only applies if your targetSdkVersion is 30. Targeting a 29 or below version, there isn't any need to all files permission as it's handled by the system.注意:这仅适用于您的 targetSdkVersion 为 30 的情况。针对 29 或以下版本,不需要all files permission ,因为它由系统处理。

You can refer to storage updates and request permission according to your use case in Storage updates in Android 11 .您可以参考Android 11 中存储更新中的存储更新并根据您的用例请求权限。

Even if you are in Android 11 or above we have to ask the storage permission not the manage external storage permission in Flutter. I tried to write the files to 'android/media/packagename/' and it is working normally, but when I try to write into other locations it's not working.即使您使用的是 Android 11 或更高版本,我们也必须在 Flutter 中询问存储权限而不是管理外部存储权限。我尝试将文件写入“android/media/packagename/”并且它工作正常,但是当我尝试写入其他位置它不起作用。 I think somehow Android internally is allowing to store data only in the same package.我认为 Android 内部以某种方式允许仅在同一个 package 中存储数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM