Bug with image picker with flutter on ImageSource.Gallery - flutter

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"

Related

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

Flutter folder picker

I have a button on screen, when you click button I need to open some folder picker.
I have searched for package, but I didn't found any package that also support Android and iOS.
Does someone knows some package who will resolve this problem, or something I could use to make solution of picking folder on iOS?
EDIT:
I found way to get directory picker functionality,I didn't test on iOS but in documentation says it works on both (Android and iOS).
String path = await FilePicker.platform.getDirectoryPath();
With this line above you get new screen to select your directory and get a path like result from that Future.
This is solved using file_picker package.
Use the package file_picker.
Easy to use:
FilePickerResult? result = await FilePicker.platform.pickFiles();
if(result != null) {
File file = File(result.files.single.path);
} else {
// Do something else...
}

Image Picker is generating only image from Video in flutter?

I'm currently working with Image picker for flutter. But when I pick video with Image Picker I only get some random .jpg file instead of .mp4 file. What is causing this issue?
PickedFile video = await ImagePicker().getVideo(source: ImageSource.gallery,);
When I print the Picked file I receive following:
I/flutter (25199): File: '/data/user/0/com.example.checkshopsonline/cache/image_picker2738786264491852340.jpg'
Am I missing some configuration with ImagePicker?
I have the same issues, but I downgraded to image_picker: 0.6.2 and it is working well. But be careful the image_picker: ^0.6.7+21 return type is PickedFile but older version is File.
Reference link: https://github.com/flutter/flutter/issues/52419#issuecomment-723750599

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

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;
}

Selecting image from Gallery or Camera in Flutter

I am trying to give users a method to add a photo from their gallery or their camera but, flutter keeps crashing after the button is pressed.
I've implemented the image picker dependency as well as Dart:io, migrated to Android X but, still no luck. Here is some of my code:
class _PreferencesState extends State<_Preferences>
with TickerProviderStateMixin {
File _image;
getImage(bool isCamera) async {
File image;
if (isCamera) {
image = await ImagePicker.pickImage(source: ImageSource.camera);
} else {
image = await ImagePicker.pickImage(source: ImageSource.gallery);
}
setState(() {
_image = image;
});
}
Then called here:
FlatButton(
textColor: Theme.of(context).primaryColor,
child: Text('Use Camera'),
onPressed: () {
getImage(true);
},
),
_image == null
? Container()
: Image.file(
_image,
height: 300,
width: 300,
),
Every time I call the method I get this error:
Exception has occurred.
PlatformException (PlatformException(error, Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference, null))
I thought I changed the image type from null so it wouldn't be called but, I'm not sure what else to do here.
The crash could be due to camera permission. Image picker plugin has removed the camera permission condition. When we access camera, app asks us for permission, but this condition in not supported anymore. We need to ask the permission manually using permission_handler plugin. Read more details about this change here and see this link on how to add runtime permission using permission_handler. Hope this resolves your issue.
This might not be the best way to do it but if you want to solve the solution more fast and easy you can just clean all your packages and delete the project then build it again. Use the commands below for the whole process:
Navigate to the folder with the pubspec.yaml file and perform the functions below on your terminal:
[user#machine~]$ flutter clean
[user#machine~]$ adb uninstall com.example.myfirst
[user#machine~]$ flutter build apk
[user#machine~]$ flutter pub get
[user#machine~]$ flutter doctor -v
[user#machine~]$ flutter pub upgrade --major-versions
[user#machine~]$ flutter run
When the the android sdk phone starts ensure you allow the app to access your phone files when you click the button to upload images/files from the gallery
N/B: This worked for my app may it might work on yours or not.