dart 存储权限请求在Flutter中不起作用

ee7vknir  于 2023-02-01  发布在  Flutter
关注(0)|答案(1)|浏览(185)

我正在尝试从Flutter访问Android模拟器上的存储。下面是我测试它的代码:

void test() async {
     PermissionStatus storageStatus = await Permission.storage.request();
     if (storageStatus == PermissionStatus.granted) {
         print("granted");
     }
     if (storageStatus == PermissionStatus.denied) {
         print("denied");
     }
     if (storageStatus == PermissionStatus.permanentlyDenied) {
         openAppSettings();
     }
 }

我已经在AndroidManifest.xml中包含了"android.permission.WRITE_EXTERNAL_STORAGE"权限。
我试过给相机权限,它的工作,但它不工作的存储。
当我检查存储权限状态时,它被永久拒绝,当应用程序的设置打开时,它显示"没有请求权限"。
我试过运行"flutter clean",这在更新清单时似乎是必要的。
我试过从手机上卸载并重新安装应用程序
我该怎么补救?

qcbq4gxm

qcbq4gxm1#

试着去做
添加到您的清单中

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

用于请求许可。

Future<bool> requestPermission(Permission permission) async {
  if (await permission.isGranted) {
    return true;
  } else {
    var result = await permission.request();
    if (result == PermissionStatus.granted) {
      return true;
    }
  }
  return false;
}

对于检查权限,您还可以根据需要更改对话框文本

Future<bool> checkPermission() async {
    ///For Check permission..
    if (Platform.isAndroid ? !await requestPermission(Permission.storage) && !await requestPermission(Permission.manageExternalStorage) : !await requestPermission(Permission.storage)) {
      await Get.dialog(CupertinoAlertDialog(
        title: const Text("Photos & Videos permission"),
        content: const Text(" Photos & Videos permission should be granted to connect with device, would you like to go to app settings to give Bluetooth & Location permissions?"),
        actions: <Widget>[
          TextButton(
              child: const Text('No thanks'),
              onPressed: () {
                Get.back();
              }),
          TextButton(
              child: const Text('Ok'),
              onPressed: () async {
                Get.back();
                await openAppSettings();
              })
        ],
      ));
      return false;
    } else {
      return true;
    }
  }

Use like this

我希望这些东西能解决你的问题

相关问题