'In flutter when camera is opened from image picker the app "Lost connection to device."
When I go through opening up the camera to set profile image using image picker, the app crashes and I lost connection to device
When pick up image from gallery its working fine,
my pubspec.yaml
name: flutterapp
description: A new Flutter project.
# Prevent accidental publishing to pub.dev.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
connectivity: ^3.0.6
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
font_awesome_flutter:
geolocator: ^7.7.0
http: ^0.13.4
image_picker: ^0.8.4+3
local_auth: ^1.1.8
path_provider: ^2.0.5
pdf: ^3.6.0
permission_handler: ^8.2.5
shared_preferences: ^2.0.8
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
flutter:
uses-material-design: true
# Enable generation of localized Strings from arb files.
generate: true
assets:
# Add assets from the images directory to the application.
- assets/
Solution 1:[1]
I had the same problem and i check the state of memory, and yes, this case happen beacuse the memory Ram of the device is full and close the task automaticly, so .
I check the state memory in the observatory of flutter, you press "Ctrl + Shift + P" when the aplication is running and write observatory for see the state of memory of the device enter image description here enter image description here
I defined the cacheHeight and cacheWidth with the atribbute of Image.file widget and i modified the filterQuality atributte in none:
Image.file( File(path), filterQuality: FilterQuality.none, cacheHeight: 66, cacheWidth: 50, )
Excuseme for my english.
Solution 2:[2]
Run your code on another device then it can work properly. The first time I used 'Xiaomi Note 7 pro' but shows the same error after run the code on another device its work properly. Also, you can update the version of your image_picker.
It's worked for me and I used:
image_picker: ^0.8.4+1
Flutter version: 2.2.3
void _pickImageCamera() async {
final picker = ImagePicker();
final pickedImage =
await picker.pickImage(source: ImageSource.camera, imageQuality: 90);
final pickedImageFile = File(pickedImage!.path);
setState(() {
_pickedImage = pickedImageFile;
});
Navigator.pop(context); }
Solution 3:[3]
Try below code hope its helpful to you. In below code I used picked image using Camera as well as Gallery
Declare File type form dart.io package
File? imagePicked;
Create Function for pick up the image from Gallery
void gallaryImage() async {
final picker = ImagePicker();
final pickedImage = await picker.pickImage(
source: ImageSource.gallery,
);
final pickedImageFile = File(pickedImage!.path);
setState(() {
imagePicked = pickedImageFile;
});
}
Create Function for pick up the image from Camera
void cameraImage() async {
final picker = ImagePicker();
final pickedImage = await picker.pickImage(
source: ImageSource.camera,
);
final pickedImageFile = File(pickedImage!.path);
setState(() {
imagePicked = pickedImageFile;
});
}
Create your Widget
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextButton.icon(
onPressed: cameraImage,
label: Text(
'Camera',
style: TextStyle(
color: Colors.black,
),
),
icon: Icon(
Icons.camera,
),
),
TextButton.icon(
onPressed: gallaryImage,
label: Text(
'Gallery',
style: TextStyle(
color: Colors.black,
),
),
icon: Icon(Icons.image),
),
],
),
Solution 4:[4]
If you are using an iOS device then you have to add permission for image picking. Add these line in your info.plist file.
<key>NSCameraUsageDescription</key>
<string>Access to take a photo by camera</string>
<key>NSAppleMusicUsageDescription</key>
<string>Access to pick a photo</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Access to pick a photo</string>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CARLOS ANDRES REY BERMUDEZ |
| Solution 2 | |
| Solution 3 | Ravindra S. Patil |
| Solution 4 | Nazmul Hasan |

