Cordova Image Picker Request Permission Issue

63 views Asked by At

I'm having an issue where I'm using cordova image picker as below

import {ImagePicker, ImagePickerOptions} from '@awesome-cordova-plugins/image-picker/ngx';

Everything run smoothly but at some point, there are some android phone does not popup the permission to access photo library. Hence, the users are unable to choose image from their library. I tried to debug it, it seems when running hasReadPermission(), the result turns out it is automatically granted the access.

What seems to be the problem? Here's my code sample

async pickImagesFromLibrary() {
    try {
      let hasPermission = await this.imagepicker.hasReadPermission();
      let test = await this.imagepicker.requestReadPermission();
      console.log(test + " Check Permission");
      
      // Check if permission is granted
      if (!hasPermission) {
        console.log('Permission not granted');
        const result = await this.imagepicker.requestReadPermission();
        console.log(result);
        
        // Check if permission is now granted
        if (result !== 'OK') {
          console.log('Permission not granted');
          this.presentAlert(
            'Permission Required',
            'Please grant permission to access images.'
          );

          return; // Exit the function if permission is not granted
        }
      } else {
        const options: ImagePickerOptions = {
          quality: 15,
          maximumImagesCount: 1,
          outputType: 1,
        };
        const imageresult = await this.imagepicker.getPictures(options);

        // Check if imageresult is empty (user clicked cancel)
        if (!imageresult || imageresult.length === 0) {
          console.log('User clicked cancel');
          return; // Exit the function
        } else {
          let userInfo = await this.storage.get('user_info_storage');
          let profile_id = userInfo.ui_id;
          let image64 = imageresult[0];
          let icNo = userInfo.ui_icno;

          var formData = new FormData();
          formData.append('user_id', profile_id);
          formData.append('image64', image64);
          formData.append('icNo', icNo);
          formData.append('camera', '0');

          this.accessProvider
            .postData(formData, 'app/authentication/uploadProfilePicture')
            .subscribe(
              (response: any) => {
                if (response.valid == true) {
                  console.log('result here : ' + response);

                  this.presentAlert('Success', response.message);
                  this.storage.set('user_profile_image', {
                    profile_image: response.image_path,
                  });
                  this.imageurl = response.image_path;
                } else {
                  this.presentAlert('Failed', response.message);
                }
              },
              (error) => {
                console.error('Error posting data:', error);
              }
            );
        }
      }
    } catch (error) {
      console.log('Image Picker Error:', error);
    }
  }
0

There are 0 answers