Image_picker, how to use ImageSource Camera and Gallery in one function - flutter

I have implemented the possibility to take picture but what I want is to offer the user both options just like in other apps (e.g. WhatsApp) where you can choice between the Camera or Gallery.
I looked on the Image_picker documentation but didn't find anything. Am I missing something or there is no way to achieve it with this plugin?

I supposed you can create multiple different widgets (buttons) that access the different source of images? Not sure if it's the best way though.
Future getImage(ImageSource imageSource) async {
// ImageSource.camera or ImageSource.gallery
File image = await ImagePicker.pickImage(source: imageSource);
return image;
}

Related

Flutter image_picker pre-select images

I'm using the flutter image_picker plugin. I can use
final ImagePicker picker = ImagePicker();
final List<XFile> images = await picker.pickMultiImage(imageQuality: 60);
After a user has picked multiple images for their post and exit out, they can always go back and select more images - in which case I want the images already selected to be "pre-selected". The API has no way to do this I think, so I'm wondering if there is a similar/reliable plugin that can do this and is well supported.
I've googled and found multiple plugins for image picking - and none of them seem to support this feature. I'm tempted to try and roll my own but would prefer to find a plugin if one exists. Any recommendations I could try?

How to share files with a different extension

As I said in the title, I'm trying to share files converting an image file to PNG format. I have tried to use the packages share_plus and social_share. I have this code:
img.Image? image = img.decodeImage(File(imageXFile.path).readAsBytesSync());
final imageFile = File('${appDir.path}/$fileName.png');
imageFile.writeAsBytesSync(img.encodePng(image));
setState(() {
_items.add(new Item(fileName, imageFile));
});
I'm trying to share the file using imageFile.path. I also tried including the image/png as mimeType, but still get the original file name and extension when I share the file.
UPDATE: I tried to find a solution for this bug, but nothing yet. It appears that is not a share_plus issue, image package neither. I printed the uri and mimeType in share_plus Kotlin code before start the activity and I got the right infos (png mimetype and the .png extension). I don't know how to proceed now.
My last (and unsuccessful) test was this:
final XFile? imageXFile = await _picker.pickImage(source: ImageSource.gallery);
final imageFile = File('${appDir.path}/$fileName.png');
imageFile.writeAsBytesSync(await imageXFile.readAsBytes());
Share.shareFiles([imageFile.path], mimeTypes: ["image/png"]);
I tried to pick a PNG image, save it and share it immediately, but still share the image as JPG.
OBS: While I was writing this update, I noticed that when I share with Telegram (as uncompressed file), it will be sent as JPG. When I share with Outlook, it is attached as PNG (with the name and extension that I want to).

How to make pickedImage from gallery or camera to effect immediately to all the pages in flutter?

I picked the image from camera and gallery in the edit profile page and I need to make it effective to the dashboard and drawer of the app immediately and also when user closes his app and comes back the photo must be there in the app. How can I achieve this.
Now I will store the image in a variable of type File, but it doesn't effect immediately and also it collpases when app is closed.
The Image you are picking, store the image in a variable of type File in setState({}).
First, your problem is divided into two parts:
Once the image is picked, it should appear in the different
places in your app.
One way to do that using the provider package as state management. Then create a class that extends change notifier. after that define a function that updates the image and notifies listeners. Something like this
class YourClass extends ChangeNotifier {
String? _image;
String? get image => _image;
void updateImageAndSave(String providedImage) {
_image = providedImage;
saveTolocaleStorage(providedImage); //To save your image to any locale storage of your prefrence.
notifyListeners();
}
void updateImage(String providedImage) {
_image = providedImage;
notifyListeners();
}
}
Note I use string because I actually provide the path, not the image itself. but you can change that.
After that, you use the consumer widget right above anywhere you want to be updated once the updateImage() function is called.
Note the image is nullable so you need to check if the image is null to show your temporary widget(your placeholder).
Read the image from your local storage.
Once you have done the above part, read the image from your locale storage on the app startup. Then, update the image in your change notifier. This will display the image at all the different places you specified in your app

How to share image + text both to WhatsApp from flutter app

I try to use this package
share_plus
from flutter to make user can share image + text with other apps (WhatsApp). It works fine on Android, but the problem is in IOS I can't share image and text same in one time. I searched for a long time but could not find a solution to this problem.
Code:
Future ShareImage()async{
var urls='https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png';
final url=Uri.parse(urls);
final res =await http.get(url);
final bytes =res.bodyBytes;
final temp = await getTemporaryDirectory();
final path ='${temp.path}/imageToShare.jpg';
File(path).writeAsBytesSync(bytes);
Share.shareFiles([path],text:'Great picture'); }
Anyone have a way to solve this problem?
I think you need these packages for your need according.
whatsapp_share2: https://pub.dev/packages/whatsapp_share2
whatsapp_share: https://pub.dev/packages/whatsapp_share
akvelon_flutter_share_plugin:
https://pub.dev/packages/akvelon_flutter_share_plugin
flutter_share: https://pub.dev/packages/flutter_share
More package available at https://pub.dev

Bug with image picker with flutter on ImageSource.Gallery

Info:
Package: image_picker plugin for flutter, version 0.6.3+1
Android build only, no IOS
Problem:
This is my method to pick an image:
Future<void> pickImage(ImageSource source) async {
File selected = await ImagePicker.pickImage(source: source);
print(selected?.path);
imageFilePath = selected?.path ?? imageFilePath;
}
=> When using ImageSource.gallery, when choosing a picture which is not in cache, 'selected.path' prints null. When selecting a picture which is in cache, it does retrieve it, 'selected.path' prints:
/data/user/0/be.etnic.parrainage_mcf/cache/image_picker2517179621202627006.jpg
Anyone knows what causes this problem and how I can solve it?
Sidenotes:
I can also pick an image by making a picture directly with ImageSource.camera, this doesn't give me any problems.
I'm not 100% sure that the selected pictures that return null
are not in cache, but the pictures that do return correctly from
choosing from the ImageSource.gallery all come from that
cache-folder
I don't have any permissions set in my AndroidManifest.xml
(other than Internet permission)
Based on this link https://github.com/flutter/flutter/issues/41459#issuecomment-563986851, following should solve the problem:
android:requestLegacyExternalStorage="true"