Select Image from Gallery Using Image Picker plugin in flutter
A Flutter plugin for iOS and Android for picking images from the image library, and taking new pictures with the camera.
Handling MainActivity destruction on Android #
Installation #
iOS #
Add the following keys to your Info.plist file, located in
<project root>/ios/Runner/Info.plist
:NSPhotoLibraryUsageDescription
- describe why your app needs permission for the photo library. This is called Privacy - Photo Library Usage Description in the visual editor.NSCameraUsageDescription
- describe why your app needs access to the camera. This is called Privacy - Camera Usage Description in the visual editor.NSMicrophoneUsageDescription
- describe why your app needs access to the microphone, if you intend to record videos. This is called Privacy - Microphone Usage Description in the visual editor.
Android #
No configuration required - the plugin should work out of the box.
Example:-
import
'package:flutter/material.dart'
;
import
'package:image_picker/image_picker.dart'
;
import
'dart:io'
;
class
PickImageDemo
extends
StatefulWidget {
PickImageDemo() :
super
();
final
String title =
"Flutter Pick Image demo"
;
@override
_PickImageDemoState createState() => _PickImageDemoState();
}
class
_PickImageDemoState
extends
State<PickImageDemo> {
Future<File> imageFile;
pickImageFromGallery(ImageSource source) {
setState(() {
imageFile = ImagePicker.pickImage(source: source);
});
}
Widget showImage() {
return
FutureBuilder<File>(
future: imageFile,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if
(snapshot.connectionState == ConnectionState.done &&
snapshot.data !=
null
) {
return
Image.file(
snapshot.data,
width:
300
,
height:
300
,
);
}
else
if
(snapshot.error !=
null
) {
return
const
Text(
'Error Picking Image'
,
textAlign: TextAlign.center,
);
}
else
{
return
const
Text(
'No Image Selected'
,
textAlign: TextAlign.center,
);
}
},
);
}
@override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
showImage(),
RaisedButton(
child: Text(
"Select Image from Gallery"
),
onPressed: () {
pickImageFromGallery(ImageSource.gallery);
},
),
],
),
),
);
}
}
Android system -- although very rarely -- sometimes kills the MainActivity after the image_picker finishes. When this happens, we lost the data selected from the image_picker. You can use
retrieveLostData
to retrieve the lost data in this situation. For example:Future<void> retrieveLostData() async {
final LostDataResponse response =
await ImagePicker.retrieveLostData();
if (response == null) {
return;
}
if (response.file != null) {
setState(() {
if (response.type == RetrieveType.video) {
_handleVideo(response.file);
} else {
_handleImage(response.file);
}
});
} else {
_handleError(response.exception);
}
}
There's no way to detect when this happens, so calling this method at the right place is essential.
Comments