image_picker: ^0.7.2+1 makes the app crash

1,011

Solution 1

I too went through all of this, setting up permission handling, doing the cache fix, and a few other bits I found online, and after a week could not solve it. Image_picker still crashed my app as soon as I took the shot. My solution in the end was to use camera_camera 2.0.1. It is a terrible library name, and it doesn't include compression, but the camera interface is nicer, and it works perfectly out of the box.

import 'package:camera_camera/camera_camera.dart';

onPressed: (){
        Navigator.push( context, MaterialPageRoute(
            builder: (_) => CameraCamera(
                  onFile: (file) {
                    // Do what you like with File file
                    // I convert to base64 ready to upload
                    Navigator.pop(context);
                  },
                )))
        },

Solution 2

Add android:requestLegacyExternalStorage="true" as an attribute to the <application> tag in AndroidManifest.xml. The attribute is false by default on apps targeting Android Q.

Share:
1,011
Tarun Jain
Author by

Tarun Jain

I am a Front End developer with experience in building websites and apps also specialised in JavaScript and have professional experience working with flutter. I also have experience working with HTML, CSS, Bootstrap and C/C++.

Updated on December 28, 2022

Comments

  • Tarun Jain
    Tarun Jain over 1 year

    i am using image_picker: ^0.7.2+1 here in my app

    i am using this code for opening the camera

         File _image;
      final picker = ImagePicker();
    
      Future getImage() async {
        final pickedFile = await picker.getImage(source: ImageSource.camera);
    
        setState(() {
          if (pickedFile != null) {
            _image = File(pickedFile.path);
          } else {
            print('No image selected.');
          }
        });
      }
    

    also Added these Permissions in android/app/src/main/AndroidManifest.xml

    <uses-permission android:name="android.permission.CAMERA" />
     <uses-feature android:name="android.hardware.camera" />
     <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.INTERNET"
    

    after using above package when my camera is opening immediately the app crashes and in terminal there is no error message it just says device disconnected. (by app crash i mean app restarts (from splash screen )automatically and captured image is also lost)

    this above app crashing issue comes only in android 10 or above android version also in android 10 or above (android 11) also this packages does not asks for the permission of camera in android 10 or 11 which i guess can be the actual problem behind this issue

    while i use android 8 or device with android version less then android 10 the image_picker: ^0.7.2+1 here works totally fine with same above given code (no changes are made in code just changed the device from android 11 to android 8) the app asks for permission as expected and then camera opens and successfully pic is captured and hence everything works fine nut same thing fails in android 10 or android 11

    note : i also tried using permission_handler plugin with image_picker but it also didn't worked for me

    can anyone please help me to fix this issue

    also if some one know any substitute code or package for image_picker can please tell me.. i just want to take pic from from camera in flutter

    • Sunil
      Sunil about 2 years
      Have you get the solution, I am also getting the same problem in one of redmi device.Please Help.
  • Tarun Jain
    Tarun Jain about 3 years
    firstly thanks ..but i have already added this android:requestLegacyExternalStorage="true" as an attribute to the <application> after reading documentation of image_picker but it doesn't work
  • Tarun Jain
    Tarun Jain about 3 years
    any one please suggest some other solution please