Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image_picker dose not ask for permission to use gallery and camera

Tags:

flutter

dart

image_picker widget does not ask for permission while opening the gallery and camera. what should I do in order to ask permission though I know that the image_picker package does implicitly ask for permission? but in my case, it didn't. I have tried every solution that I found on the internet .none of them can solve my problem.

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" /> -->
like image 796
PARANJAY Avatar asked Dec 05 '25 17:12

PARANJAY


1 Answers

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 ...
}
like image 54
Jamirul islam Avatar answered Dec 08 '25 15:12

Jamirul islam