ImagePicker never returning value flutter - flutter

I'm trying to prompt users of my app to upload photos. However, I'm have an issue. The function where I try to get the gallery image always returns null / never completes.
This i my code:
GestureDetector(
onTap: () async {
final image =
await ImagePicker().pickImage(source: ImageSource.gallery);
if (image != null) {
print(image.path);
print('exists');
}
},
It never prints 'exists' or image.path, I don't know whats wrong.
I have added this to info.plist
<key>NSCameraUsageDescription</key>
<string>For image upload and verification</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>For image upload and ID verification</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app uses the microphone for videos</string>
Package version: ^0.8.4
Would be really thankful for some input!

Related

Uploading image into Floating Action Button, set state not working

I have a floating action button that I want a user to click to take a picture with their camera and then have that image replace the camera icon on the floating action bar button.
Here is the code for my FAB, and including uploading the image to firestore storage.
floatingActionButton: FloatingActionButton.large(
heroTag: "add image",
backgroundColor: const Color(0xFF93C3B9),
child: (imageURL == ' ')
? const Icon(Icons.add_a_photo_outlined)
: Image.network(imageURL),
//open add gear page
onPressed: () async {
// todo: upload an image to Firebase Storage
//Take picture
ImagePicker imagePicker = ImagePicker();
XFile? file = await imagePicker.pickImage(source: ImageSource.camera);
if (file == null) return;
String uniqueFileName =
DateTime.now().millisecondsSinceEpoch.toString();
//Get reference to storage root
Reference referenceRoot = FirebaseStorage.instance.ref();
Reference referenceDirImages = referenceRoot.child('images/$userID');
Reference referenceImageToUpload =
referenceDirImages.child(uniqueFileName);
try {
//upload image
await referenceImageToUpload.putFile(File(file.path));
//get download URL
setState(() async {
imageURL = await referenceImageToUpload.getDownloadURL();
print(imageURL);
});
//upload path to fireStore database
} catch (error) {}
},
),
After the image uploads it's like the set state is not working to replace the icon with the image. The odd part is is I crtl-s and save in Visual Studio Code then the widgets seem to rebuild and then the image is visible there...
So after playing around with my code a bit I decided to edit the above code and take tha await function out of the setState() and make setState() not async anymore:
//get download URL
String tempUrl = await referenceImageToUpload.getDownloadURL();
setState(() {
print("--------- Set State -----------");
imageURL = tempUrl;
print("--------- Set State end -----------");
});
print("New image url $imageURL ------------");
not sure why it works, but this solves my issue.
By your description of the issue, I think you might be using StatelessWidget instead of StatefulWidget.
You see the button change when performing a hotreload because the value of imageURL is correctly changing internally, but you need a StatefulWidget to update the UI also.
Hope it helps!

Why won't my image show up after picking from image_picker in Flutter?

I have been using image_picker: ^0.8.4+9 for the past couple of months with no problems. Today whenever I try to pick an image the gallery opens and I am able to pick an image, however once thats done the image doesn't appear on the screen. No errors and I update my github with it everyday and checked to see if something was changed but nothing was. I've tried using other versions only to have the same problem. I understand that this is an issue with M1 Macs sometimes, however why would it suddenly stop working using the same version? I have seen other questions similar like this. I have the correct permissions and there hasn't been a thing changed since it was last working. This could be a problem I cannot fix due to it being an iOS issue or something else but I wanted to see if anyone else is having this issue. Can't test on a physical device since I am on iOS beta but still it was working on the sim so that shouldn't matter.
Here is the basic function of getting am image and storing it:
File? pickedImage;
Future pickImage() async {
final _imagePicker = ImagePicker();
final pickedImage =
await _imagePicker.pickImage(source: ImageSource.gallery);
final imageTemp = File(pickedImage!.path);
setState(() => this.pickedImage = imageTemp);
}
Here is the button to initiate the gallery and pick the image:
ElevatedButton(
onPressed: () async {
final _imagePicker = ImagePicker();
final pickedImage = await _imagePicker.pickImage(
source: ImageSource.gallery);
final imageTemp = File(pickedImage!.path);
setState(() => this.pickedImage = imageTemp);
},
child: const Text('Add Image')),
const SizedBox(height: 20),
// The image should show here if not null else the 'No image' text
pickedImage != null
? Image.file(pickedImage!, height: 200, width: 200)
: Text('No Image'),
EDIT: As Vandad linked in the comment below, it seems this is a newer issue. Here is the discussion. github.com/flutter/flutter/issues/98569

Saving a Video into gallery using path_provider package

I have been Working on a small project where users can download WhatsApp status directly into their gallery. I made two tabs. one tab for WhatsApp Images and another for Videos. I could successfully download images into the gallery. now, I am facing a problem while downloading a Video into the gallery. I used path provider to download the video into the gallery. the Video gets downloaded in storage\emulated\0\android\data\com.JeevanCrasta.stikkers\files\videos\
I want to download those videos in the storage\emulated\0\Stikkr\videosdirectory. And those videos should be visible in the gallery. now those videos get downloaded in a different directory and are not visible in the gallary.
this is the code where videos get downloaded while the button is pressed :
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.teal,
child: Icon(Icons.download_sharp),
onPressed: () async {
_onLoading(true, "");
File originalVideoFile = File(widget.videoFile);
Directory directory = await getExternalStorageDirectory();
if (Directory("${directory.path.toString()}/Stikkr").existsSync()) {
Directory("${directory.path.toString()}/Stikkr")
.createSync(recursive: true);
}
String path = directory.path;
String curDate = DateTime.now().toString();
String newFileName = "$path/Stikkr/Videos/VIDEO-$curDate.mp4";
// print(newFileName);
await originalVideoFile.copy(newFileName);
_onLoading(false,
"If Video not available in gallary\n\nYou can find all videos at");
},
),
You can use package https://pub.dev/packages/gallery_saver
1 Saves images and videos from network, it has to contain http/https prefix.
2 Save temporary file to external storage
Both images and videos will be visible in Android Gallery and iOS Photos.
In your case, file has downloaded, so you can pass newFileName and use
GallerySaver.saveVideo(newFileName).then((String path) {
setState(() {
secondButtonText = 'video saved!';
});
});

Flutter image_picker 0.4.12+1 not returning from camera

I am using image_picker version 0.4.12+1 to take a picture from Camera on Android Emulator but the program never returns from ImagePicker.pickImage async call.
_takePicture() async{
print("This is executed");
var image = await ImagePicker.pickImage(source: ImageSource.camera);
print("But is this never executed");
}
This post on Github discusses the same issue and suggests a solution by modifying onMainActivity result. How can I do that? Or is there another solution that does not involve upgrading to AndroidX?
If it helps, the call will return successfully if I am using a different ImageSource.gallery instead of a camera.
Using image picker version,
image_picker: ^0.6.1+10 , this works.
Future _getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
}

How to avoid app crash when picking a image with flutter and image_picker package? [duplicate]

This question already has answers here:
How to solve the image picker crash app error?
(6 answers)
Closed 3 years ago.
I'm building a application for taking photos and send them into firestore, i'm using image_picker :0.5.0+8 when I'm trying to pick a image from camera or from gallery the app crash. it open gallery very well or the camera very well but if a choose a picture the app crash.
this is my code:
File image_state; AppState state;
bool isLoading; bool isShowSticker; String _imageUrl;
Future getImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
image_state = tempImage;
state = AppState.picked;
});
File croppedFile = await ImageCropper.cropImage(
sourcePath: image_state.path,
ratioX: 1.0,
ratioY: 1.0,
toolbarTitle: 'Cropper',
toolbarColor: Colors.blue,
);
if (croppedFile != null) {
image_state = croppedFile;
setState(() {
state = AppState.cropped;
});
} }
is there some one who work with this librairy? can you show me an example that work please?
issue
There are example of usages inside the plugin's repo.
It may be related to AndroidX compability. You have to avoid AndroidX or migrate your project to AndroidX.
There were a breaking change after version 0.5.0