flutter image_picker不请求使用图库和照相机权限

n3ipq98p  于 2022-12-24  发布在  Flutter
关注(0)|答案(4)|浏览(515)

image_picker小部件在打开画廊和照相机时不请求权限.我应该做什么以便请求权限尽管我知道image_picker软件包确实隐式请求权限?但在我的情况下,它没有.我已经尝试了每一个解决方案,我发现在互联网上.没有一个能解决我的问题.
pubspec.yml

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

AndroidMenifest.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" /> -->
k7fdbhmy

k7fdbhmy1#

使用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);
    }
  }
ma8fv8wu

ma8fv8wu2#

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 ...
}
kxkpmulp

kxkpmulp3#

如果您向“AndroidManifest.xml”文件添加权限,则当您尝试使用图像拾取器时将自动询问权限。我使用相机和拾取器库,因此我将这些权限放在“AndroidManifest.xml”中。

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

y1aodyip

y1aodyip4#

你不需要请求画廊的权限,只需要请求相机的权限,你会自动获得画廊的权限,你可以使用它。

相关问题