13

I'm currently using these versions: flutter : 2.16.0 image_picker : ^0.8.4+7

The image picker is not working. After running the app, when I click on the button to activate the pickImage function, running suddenly stops and the app crashes and stops. On debug, the only message I get is:

Lost connection to device.

Here's the code:

import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:hipocampus_editors/widgets/textformfield_widget.dart';
import 'package:image_picker/image_picker.dart';

class AddSystemPage extends StatefulWidget {
  const AddSystemPage({Key? key}) : super(key: key);

  @override
  _AddSystemPageState createState() => _AddSystemPageState();
}

class _AddSystemPageState extends State<AddSystemPage> {
  final _formKey = GlobalKey<FormState>();
File? image1;

  Future pickImage() async{
    
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null)  return;
final imageTemporary = File(image.path);
setState(() {
  image1 = imageTemporary;
});

    } 
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: Scaffold(
        appBar: AppBar(title: const Text('System',),),
        
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 5),
            child: Form(
              key: _formKey,
              child: Container(
                width: MediaQuery.of(context).size.width,
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: SingleChildScrollView(
                    child: Column(
                  children: [
                    
                    ElevatedButton(onPressed: (){
                      pickImage();
                                          }, child: Text('Select image'))
                    
                  ],
                )),
              ),
            ),
          ),
        ),
      ),
    );
    }
    }
2
  • are you added "android:requestLegacyExternalStorage="true" " in manifest.xml?? Commented Feb 10, 2022 at 5:10
  • @RohitChaurasiya Have you resolved this issue I'm also having this issue in android 12 Commented Nov 25, 2022 at 13:51

5 Answers 5

18

Add this to iOS>runner>Info.plist

<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library</string>
Sign up to request clarification or add additional context in comments.

1 Comment

I also added this <key>NSCameraUsageDescription</key> <string>Need to access camera in order to get image.</string>
4

Please check if you have registered the ImagePicker Correctly.

and if you have registered ImagePicker correctly then put a try and catch blog in the pick Image Function to get more debug information:

Future pickImage() async{
    
  try{
    final image = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (image == null)  return;
    final imageTemporary = File(image.path);
    setState(() {
      image1 = imageTemporary;
    });
  } catch(error) {
    print("error: $error");
  }

} 

Comments

1

This happened when I was trying with iOS simulator. You need to add these keys in /ios/Runner/Info.plist:

NSPhotoLibraryUsageDescription NSCameraUsageDescription

Also try to test your app in physical device. And if you working for Android version then no configuration changes are required and you can refer to this video: https://youtu.be/s0YqbEJcRtE

1 Comment

you can use camera: ^0.9.5 its a better option
0

he image_picker package causes the app to crash when using the camera, but picking from the gallery works fine. So, I want to use the camera package for capturing photos and use image_picker only for selecting images from the gallery

3 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Is this answer a new question? If you can write a clear question, there is no reputation requirement to ask it.
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
-2

For developing on apple silicon (M1), I needed to remove the google_maps plugin. This plugin moves the simulator to Rosetta and this has some issues with image handling in the simulator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.