I was using the following code to pick images from both Camera and Gallery.
Future<File> getImageFromGallery() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
_image = File(pickedFile.path);
return _image; }
Future<File> getImageFromCamera() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
_image = File(pickedFile.path);
return _image; }
Used Package
import 'package:image_picker/image_picker.dart';
Everything was perfect until I updated flutter to version 1.22.1
After that when I pickup an image the app crashes without any specific error, all I got is
Lost connection to device
Thanks
Related
Following a tutorial for adding and saving images and getting errors for the below code, under ImagePicker, ImageSource and SelectedImage.
Future getImage() async {
var image = await ImagePicker.PickImage(source: ImageSource.camera);
setState(() {
selectedimage = image;
});
}
You need to create an instance first, then you will be able to use pickImage method.
Future getImage() async {
XFile? image = await ImagePicker().pickImage(source: ImageSource.camera);
if (image != null) {
setState(() {
selectedimage = image;
});
}
}
Fine more about image_picker
You have to be careful whenever playing with the setState() functions. This combined with how you pick the image and how you store it in the 'var' in your case could cause further trouble. To generally ease things up for further use I recommend creating a utils class, in which you would have an image picker method, just like this one:
import 'package:image_picker/image_picker.dart';
pickImage(ImageSource source) async {
final ImagePicker _imagePicker = ImagePicker();
XFile? _file = await _imagePicker.pickImage(source: source);
if (_file != null) {
return await _file.readAsBytes();
}
print('No Image Selected');
}
Afterwards, if you would like to call that in any other instance you would need something like this, though this is for a url image(I had it in hand), such as:
void selectImage() async {
Uint8List? im = await pickImage(ImageSource.gallery);
final ByteData imageData = await NetworkAssetBundle(Uri.parse(
"url for template image"))
.load("");
final Uint8List bytes = imageData.buffer.asUint8List();
// if null - use the template image
im ??= bytes;
// update state
setState(() {
_image = im!;
});
}
Hope it helped in a way.
I was trying to upload a image from camera. I managed to get the image from the camera but when after user picks it, it doesn't show up in the Firebase Storage. Can you help me to find what's wrong
var _instance = FirebaseFirestore.instance;
FirebaseAuth auth_ = FirebaseAuth.instance;
File? image;
String? downloadLink;
Future pickImage() async {
var fileToUpload =
await ImagePicker().pickImage(source: ImageSource.camera);
if (image == null) return;
setState(() {
image = File(fileToUpload!.path);
});
Reference referenceWay = FirebaseStorage.instance
.ref()
.child('profilePics')
.child(auth_.currentUser!.uid)
.child("profilPic.png");
UploadTask uploadTask = referenceWay.putFile(image!);
TaskSnapshot downloadURL = (await uploadTask);
String url = await downloadURL.ref.getDownloadURL();
}
Required dependencies:
dependencies:
firebase_storage: ^3.0.8
firebase_core: ^0.4.0+9
firebase_analytics: ^5.0.6
image_picker:
Checkthis:
final picker = ImagePicker();
Future pickImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_imageFile = File(pickedFile.path);
});
}
File _imageFile;
Future uploadImageToFirebase(BuildContext context) async {
String fileName = basename(_imageFile.path);
StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child('uploads/$fileName');
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
taskSnapshot.ref.getDownloadURL().then(
(value) => print("Done: $value"),
);
}
for iOS:
Add permissions in iOS Info.list
<key>NSCameraUsageDescription</key>
<string>Need to access your camera to capture a photo add and update profile picture.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Need to access your photo library to select a photo add and update profile picture</string>
Sample Code Example.
After the update in Image_Picker, "XFile" is used instead of "File" and "pickImage(...)" is used instead of "getImage(...)".
await _picker.pickImage(source: ImageSource.camera)
this code now returns XFile.
Related codes are below
XFile _image;
final ImagePicker _picker = ImagePicker();
Future getImageFromCamera() async {
final pickedFile = await _picker.pickImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_image = pickedFile;
} else {
print("No image selected");
}
});
}
Future<void> uploadImageToStorage(XFile imageFile) async {
String imageName = "${AuthService().getCurrentUID()}";
FirebaseStorage.instance
.ref()
.child("photos")
.child(imageName)
.putFile(_image);
}
error
The argument type 'XFile' can't be assigned to the parameter type 'File'.
I can't write "_image" in putFile(...). It asks me for "File" type. But _image is of type "XFile".
How can I upload an image into Firebase Storage?
import 'dart:io' as i;
Future<void> uploadImageToStorage(XFile imageFile) async {
String imageName = "${AuthService().getCurrentUID()}";
FirebaseStorage.instance
.ref()
.child("photos")
.child(imageName)
.putFile(i.File(_image.path));
}
You need to convert it into a file object using File from the XFile's path.
I am trying to get a image using camera in flutter using image picker but i am getting an error
lib/Screen/AllDocuments.dart:15:3: Error: 'File' isn't a type.
File _image;
I have also tried to cast but still it is not working
File _image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_image = File(pickedFile);
});
}
How to fix this error
You have to call the .path method on the pickedFile.
See this documentation here => Image Picker
Use this :
File _image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
_image = File(pickedFile.path);
});
}
I am attempting to cast an Image to a File in Flutter. Is there a method for this?
This is how to pick an image as a file
File _image;
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
_image = File(pickedFile.path);
});
}
Also We can convert this image as Bytestream
var convertedImage;
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
convertedImage = await pickedFile.readAsBytes();
convertedImage = await decodeImageFromList(convertedImage);
setState(() {
convertedImage = convertedImage;
});
}
Hope this helps!