Save Image From ImagePicker Locally as a Memory(cache) - flutter

I want to save an Image from ImagePicker as a Memory but error Occured . Can you Please help me with this function and if another function needed to load image please Mentioned it below.
Uint8List? memoryImage;
Future getImage() async {
final picker = ImagePicker();
final image = await picker.getImage(source: ImageSource.camera);
if (image == null) return;
final Directory directory = await getApplicationDocumentsDirectory();
final path=directory.path;
final Filename=basename(image.path);
File file = File('$directory/$Filename.jpg');
final bytes = await file.readAsBytes();
final byte1= file.writeAsBytes(bytes);
setState(() {
memoryImage = byte1 as Uint8List?;
});
}

With this line you can write image bytes as a file.
File imageFile = await File(fileSavePath).writeAsBytes(imageBytes);
To access the Uint8List from the file you need to use
Uint8List memoryImage = File(imagePath).readAsBytesSync();
Or
Uint8List memoryImage = await File(imagePath).readAsBytes();
here the problem in your code is you are assigning file to a Uint8List. That's the error I guess

Related

Flutter Newb, anyone tell me whats wrong with this code?

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.

Save ImagePicker Image in Shared Preferences - Flutter

I'm trying to save image picked from ImagePicker and store it in shared preferences and then retrieve it from there but no luck so far
To make my question more specific, how to save an image as a string in shared preference and then later retrieve it
Here is my code
File? profileImage;
void saveData(String key, String value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
void getData(String key) async {
final prefs = await SharedPreferences.getInstance();
final image = await prefs.getString(key);
setState(() {
profileImage = image; //this would result into error because profileImage expect file type value
});
}
Future pickProfile() async {
final profileImagePicker = await ImagePicker().pickImage(source: ImageSource.gallery);
final File profile = File(profileImagePicker!.path);
final directoryPath = await getApplicationDocumentsDirectory();
final path = directoryPath.path;
final imageFile = await File(profileImagePicker.path).copy('$path/image1.png'); // What am I supposed to do after this step
saveData('profile', path); what value needs to be stored here, it expects a string
setState(() {
profileImage = profile;
});
}
To convert image into String you can use below code
final bytes = imageFile.readAsBytesSync();
String imageString = base64Encode(bytes);
To convert String to Image
Uint8List bytes = BASE64.decode(base64ImageString);
You can use the Image widget to diplay the Image
Image.memory(bytes);

A value of type 'XFIle' can't be assigned to a variable of type 'File' error

I am using image_picker: ^0.8.4+4 where I am getting this error. what I can do to make this code right?
late File selectedImage;
bool _isLoading = false;
CrudMethods crudMethods = CrudMethods();
Future getImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
selectedImage = image; //A value of type 'XFIle' can't be assigned to a variable of type 'File' error.
});
}
uploadBlog() async {
// ignore: unnecessary_null_comparison
if (selectedImage != null) {
setState(() {
_isLoading = true;
});
you can conver XFile to File using following line:
selectedImage = File(image.path);
First, you should create your variable as XFile
Because this is what you get from image picker.
XFile photo;
void _pickImage() async {
final ImagePicker _picker = ImagePicker();
photo = await _picker.pickImage(source: ImageSource.camera);
if (photo == null) return;
}
And then you can use your image as a file image.
Image.file(File(photo.path))
This happens because the package (image_picker ) you are using is relying on XFile and not File, as previously did.
So, first you have to create a variable of type File so you can work with later as you did, and after fetching the selectedImage you pass the path to instantiate the File. Like this:
File? selectedImage;
bool _isLoading = false;
CrudMethods crudMethods = CrudMethods();
Future getImage() async {
var image = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
selectedImage = File(image!.path); // won't have any error now
});
}
//implement the upload code
XFile and File can be converted to each other like the following in Flutter:
import 'dart:io';
import 'package:camera/camera.dart';
XFile takenPhotoXFile = await _controller!.takePicture();
// XFile to File
File photoAsFile = File(takenPhotoXFile.path);
// File to XFile
XFile imageFileAsXFile = XFile(photoAsFile.path);

Flutter image picker not providing the image 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);
});
}

how to convert image captured with camera to base64 with flutter

I want to convert image captured with camera to base64 but it seems am converting the image path and not the image itself, please help me.
File file;
final picker = ImagePicker();
void _choose() async {
final pickedFile = await picker.getImage(source: ImageSource.camera);
file = File(pickedFile.path);
if (file != null) {
setState(() {
base64Encode(file.readAsBytesSync());
print(base64Encode(Image.file(file));
});
}
}
Wrong:
print(base64Encode(Image.file(file));
Right:
print(base64Encode(file.readAsBytesSync()));