You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android模拟器授予相册权限后仍提示需相册权限的问题咨询及权限疑问

Android模拟器授予相册权限后仍提示需相册权限的问题咨询及权限疑问

我最近在Android模拟器上测试图片选择与上传功能时碰到了一个费解的问题:明明刚在系统设置里给应用开了相册和视频的权限,可一触发相册选择操作,就立刻弹出提示“Please grant Photo access permission to select images”。我反复检查了权限请求的代码逻辑,没看出明显问题,会不会是模拟器本身的bug?有没有同行遇到过类似的情况呀?

补充一下当前的权限状态,在Android系统设置里能清楚看到:

Allowed: Photos and Videos
Not allowed: Camera

以下是我相关的权限检查与图片处理核心代码:

相册选择回调处理

void _onPhotosSelectedFromGallery(List<AssetEntity> selectedPhotos) {
  if (selectedPhotos.isEmpty) return;
  setState(() {
    _selectedAssets = selectedPhotos;
    _isUploading = true;
  });
  _uploadSelectedPhotos(selectedPhotos);
}

相机拍照逻辑

Future<void> _takePhoto() async {
  final hasPermission = await _checkAndRequestCameraPermission();
  if (!hasPermission) {
    _showPermissionDeniedDialog("Camera");
    return;
  }
  try {
    final ImagePicker picker = ImagePicker();
    final XFile? photo = await picker.pickImage(
      source: ImageSource.camera,
      imageQuality: 70, // Compress during capture
    );
    if (photo != null) {
      setState(() => _isUploading = true);
      await _processAndUploadImageFile(photo);
    }
  } catch (e) {
    _showErrorSnackBar("Error taking photo: $e");
  } finally {
    setState(() => _isUploading = false);
  }
}

相册权限检查与请求

Future<bool> _checkAndRequestGalleryPermissions() async {
  if (Platform.isIOS) {
    final status = await Permission.photos.status;
    if (status.isGranted) return true;
    final result = await Permission.photos.request();
    return result.isGranted;
  } else if (Platform.isAndroid) {
    final deviceInfo = await DeviceInfoPlugin().androidInfo;
    if (deviceInfo.version.sdkInt >= 33) {
      final photosStatus = await Permission.photos.status;
      if (photosStatus.isGranted) return true;
      final result = await Permission.photos.request();
      return result.isGranted;
    } else {
      // Android 12 and below
      final storageStatus = await Permission.storage.status;
      if (storageStatus.isGranted) return true;
      final result = await Permission.storage.request();
      return result.isGranted;
    }
  }
  return false;
}

相机权限检查与请求

Future<bool> _checkAndRequestCameraPermission() async {
  final status = await Permission.camera.status;
  if (status.isGranted) return true;
  final result = await Permission.camera.request();
  return result.isGranted;
}

内容来源于stack exchange

火山引擎 最新活动