如何在 Flutter 中检查权限

How to check permission in Flutter

我正在使用image_picker and permission_handler. My app gets permission through image_picker. However, when I check the status of the permission_handler,仍然显示权限被拒绝。请帮忙

Future<void> _pickImage() async{
var status1 = await Permission.photos.status;
var status2 = await Permission.mediaLibrary.status;
var status3 = await Permission.camera.status;
var status4 = await Permission.accessMediaLocation.status;
var status5 = await Permission.storage.status;
var status6 = await Permission.photosAddOnly.status;
print(status1);
print(status2);
print(status3);
print(status4);
print(status5);
print(status6);

if (status1.isDenied) {
  
  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
    content: const Text(
      'permission_denied_message',
      textAlign: TextAlign.center,
      style: TextStyle(
          color: Colors.white
      ),
    ).tr(),
    action: SnackBarAction(
      label: tr('setting'),
      textColor: Colors.yellow[600],
      onPressed: (){
        openAppSettings();
      },
    ),
    backgroundColor: Colors.blue,
    duration: const Duration(seconds: 5),
  ));
}
final pickedImage = await ImagePicker().pickImage(source: ImageSource.gallery);
imageFile = pickedImage != null ? File(pickedImage.path) : null;

_cropImage();

}

将以下内容添加到您的“gradle.properties”文件中:

android.useAndroidX=true
android.enableJetifier=true

确保将“android/app/build.gradle”文件中的 compileSdkVersion 设置为 31:

android {
  compileSdkVersion 31
  ...
}

确保替换所有 android。依赖于他们的 AndroidX 同行。 为您的 AndroidManifest.xml 文件添加权限。根据您启动应用程序的方式选择调试、主要和配置文件版本。一般只给主版本加权限即可。