pickImageFromGallery static method

Future<File?> pickImageFromGallery()

Implementation

static Future<File?> pickImageFromGallery() async {
  try {
    // For iOS, don't check permissions beforehand
    // Let the system handle it
    if (!Platform.isIOS) {
      if (!await _requestPermissions()) {
        throw Exception('Permission denied for gallery access');
      }
    }

    final XFile? image = await _picker.pickImage(
      source: ImageSource.gallery,
      imageQuality: 80,
    );

    if (image == null) {
      return null;
    }

    return File(image.path);
  } catch (e) {
    // Provide a more helpful error message
    if (e.toString().contains('Permission denied') ||
        e.toString().contains('access denied')) {
      throw Exception(
        'Gallery access denied. Please grant photo library permission in your device settings.',
      );
    }
    throw Exception('Error picking image from gallery: $e');
  }
}