【问题标题】:image_picker dose not ask for permission to use gallery and cameraimage_picker 不请求使用画廊和相机的许可
【发布时间】:2022-09-27 17:40:48
【问题描述】:

image_picker 小部件在打开图库和相机时不请求许可。 尽管我知道 image_picker 包确实隐含地请求许可,但我应该怎么做才能请求许可?但就我而言,它没有。我已经尝试了在互联网上找到的所有解决方案。没有一个可以解决我的问题。

发布规范.yaml

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  # image_picker: ^0.8.5+3
  image_picker: ^0.8.5+3

AndroidManifest.xml

 <manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"
        package=\"com.example.flutter_application_1\">
        <!-- The INTERNET permission is required for development. Specifically,
             the Flutter tool needs it to communicate with the running application
             to allow setting breakpoints, to provide hot reload, etc.
        -->
        <uses-permission android:name=\"android.permission.INTERNET\"/>
        <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />
        <uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\" />
        <uses-feature android:name=\"android.hardware.camera\" android:required=\"false\" />
    
    </manifest>
    
    <!-- <application
            android:name=\"io.flutter.app.FlutterApplication\"
            android:label=\"xxxxxx\"
            android:icon=\"@mipmap/launcher_icon\">
            <activity>
            android:name=\"io.flutter.app.FlutterApplication\"
            android:requestLegacyExternalStorage=\"true\"
    
            </activity>
    </application>
    
    <uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" />
    <uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\" /> -->
  • 如果用户没有为应用程序设置任何默认权限,图像选择器总是会请求权限。请检查您的应用程序的权限访问。
  • 应用程序设置在权限部分显示“无权限请求”

标签: flutter dart


【解决方案1】:

使用permission_handler 包:

尝试这个:

void _checkPermission(BuildContext context) async {
    FocusScope.of(context).requestFocus(FocusNode());
    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) {
      //openCameraGallery();
      //_openDialog(context);
    }
    bool isPermanentlyDenied =
        statusCamera == PermissionStatus.permanentlyDenied ||
            statusStorage == PermissionStatus.permanentlyDenied ||
            statusPhotos == PermissionStatus.permanentlyDenied;
    if (isPermanentlyDenied) {
      _showSettingsDialog(context);
    }
  }

【讨论】:

  • 仍然没有征求许可,直接打开相机和画廊。我现在该怎么办??
  • 我认为已授予权限,请在模拟器中删除应用程序并再次重建
  • 但在手机设置中显示“未请求许可”
  • 有两个 androidmanifest.xml 文件,我应该使用哪一个????/
【解决方案2】:
import 'package:permission_handler/permission_handler.dart';

Future<bool> checkAndRequestCameraPermissions() async {
  PermissionStatus permission =
      await PermissionHandler().checkPermissionStatus(PermissionGroup.camera);
  if (permission != PermissionStatus.granted) {
    Map<PermissionGroup, PermissionStatus> permissions =
        await PermissionHandler().requestPermissions([PermissionGroup.camera]);
    return permissions[PermissionGroup.camera] == PermissionStatus.granted;
  } else {
    return true;
  }
}
Then every call to the image picker has to be wrapped like this to prevent your app from crashing

if (await checkAndRequestCameraPermissions()) {
  File image = await ImagePicker.pickImage(source: ImageSource.camera);
  // continue with the image ...
}

【讨论】:

    【解决方案3】:

    您需要为“AndroidManifest.xml”文件添加权限。我使用相机和图像选择器包库,所以我使用了这些;

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.GALLERY"/>
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多