Home > Software engineering >  Why flutter in debug mode shows the permission status "denied" where its actually "pe
Why flutter in debug mode shows the permission status "denied" where its actually "pe

Time:05-20

I'm really bit confused, in order to test my app I denied the permission to photos and camera however App doesn't ask again for permission when using image picker package, And instead enable me to upload the images freely! ? any idea about app behavior in debug mode? and why permission message doesn't show again?

_getFromGallery() async {
    if (await Permission.photos.request().isGranted) {
      PickedFile? image = await ImagePicker.platform.pickImage(
          source: ImageSource.gallery);
      if (image != null) {

        /// For storing image in local memory:
        // final File file = File(image.path);
        // final Directory directory = await getApplicationDocumentsDirectory();
        // final imagepath = directory.path;
        // final String fileName = path.basename(image.path);
        // File newImage = await file.copy('$imagepath/$fileName');

        setState(() {
          _imagelocal = File(image.path);
          _uploadimage();
        });
      }
    }
    else if (await Permission.photos.request().isPermanentlyDenied) {
        openAppSettings();
        PickedFile? image = await ImagePicker.platform.pickImage(
            source: ImageSource.gallery);

        if (image != null) {


          /// For storing image in local memory:
          // final File file = File(image.path);
          // final Directory directory = await getApplicationDocumentsDirectory();
          // final imagepath = directory.path;
          // final String fileName = path.basename(image.path);
          // File newImage = await file.copy('$imagepath/$fileName');

          setState(() {
            _imagelocal = File(image.path);
            _uploadimage();
          });
        }
      }

  }

CodePudding user response:

I think you have just check one permission for photos, Try check for all permission camera, storage, photo

Example:

void _checkPermission(BuildContext context) async {
    Map<Permission, PermissionStatus> statues = await [
      Permission.camera,
      Permission.storage,
      Permission.photos
    ].request();
    PermissionStatus? statusCamera = statues[Permission.camera];
    PermissionStatus? statusStorage = statues[Permission.storage];
    PermissionStatus? statusPhotos = statues[Permission.photos];
    bool isGranted = statusCamera == PermissionStatus.granted &&
        statusStorage == PermissionStatus.granted &&
        statusPhotos == PermissionStatus.granted;
    if (isGranted) {
      _openPickImage(context);
    }
    bool isPermanentlyDenied =
        statusCamera == PermissionStatus.permanentlyDenied ||
            statusStorage == PermissionStatus.permanentlyDenied ||
            statusPhotos == PermissionStatus.permanentlyDenied;
    if (isPermanentlyDenied) {
      _showSettingsDialog();
    }
  }

CodePudding user response:

After long time of investigation I noticed the following:

1- PermissionStatusreturns always denied even though its actual status is permanentlyDenied

2- Photo permission is not available at least on my testing android device, instead it has storage permission only, where IOS has photo and doesn't have Storage permission, don't hesitate to correct me if I'm wrong?

Update Code:

Future<void> _showMyDialogStorage() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(AppLocalizations.of(context)!.permissionToStorage),
          content: Text(AppLocalizations.of(context)!.applicationneedsThispermissiontoworkproperly),
          actions: <Widget>[
            TextButton(
              onPressed: () => {
                openAppSettings(),
                Navigator.pushReplacement(
                    context,
                    MaterialPageRoute(
                        settings: const RouteSettings(
                          name: "/PostLoad",),
                        builder: (
                            BuildContext context) =>
                            PostLoad(mobile_number: widget.mobile_number,))),
              },
              child: Text(AppLocalizations.of(context)!.ok),
            ),
            TextButton(
              onPressed: () => Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                      settings: const RouteSettings(
                        name: "/PsotLoad",),
                      builder: (
                          BuildContext context) =>
                          PostLoad())),
              child: Text(AppLocalizations.of(context)!.cancel),
            ),
          ],
        );
      },
    );
  }

  // Get image from gallery and store it locally
  Future<void> _getFromGallery() async {
    var status_storage = await Permission.storage.status;
    var status_photo = await Permission.photos.status;

    if (status_storage == PermissionStatus.granted && status_photo == PermissionStatus.granted) {

      PickedFile? image = await ImagePicker.platform.pickImage(
          source: ImageSource.gallery);
      if (image != null) {

        /// For storing image in local memory:
        // final File file = File(image.path);
        // final Directory directory = await getApplicationDocumentsDirectory();
        // final imagepath = directory.path;
        // final String fileName = path.basename(image.path);
        // File newImage = await file.copy('$imagepath/$fileName');

        setState(() {
          _imagelocal = File(image.path);
          _uploadimage();
        });
      }
    }

    else if (status_storage == PermissionStatus.denied || status_photo == PermissionStatus.denied) {

      // Permission.storage.request();
      _showMyDialogStorage();
    }

    else if (status_storage == PermissionStatus.permanentlyDenied || status_photo == PermissionStatus.permanentlyDenied) {

      _showMyDialogStorage();

      }

  }
  • Related