user14953411
user14953411

Reputation:

Flutter Image_Picker doesn't pick image from gallery and return to app

Here is a what's happening : I'm trying to upload an image from gallery to my app on iOS simulator. Image Picker opens the gallery but can't select an image and return to app. Here's my simple code:

  File _image;
  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });
  }

and my widget:

body: Center(
        child: _image != null ? Image.file(_image) : Text('no Image'),
),

Thank you all in advance

Upvotes: 4

Views: 17049

Answers (5)

Valkyrie
Valkyrie

Reputation: 109

For everyone still dealing with this problem

Please not this in the image_picker documentary and make sure you tested it on a real device:

Starting with version 0.8.1 the iOS implementation uses PHPicker to pick (multiple) images on iOS 14 or higher. As a result of implementing PHPicker it becomes impossible to pick HEIC images on the iOS simulator in iOS 14+. This is a known issue. Please test this on a real device, or test with non-HEIC images until Apple solves this issue

Upvotes: 0

Dinesh Prajapati
Dinesh Prajapati

Reputation: 47

You need to add the user permissions(key) and add the purpose(values) of these in info.plist file in iOS module.

Add the following keys to your Info.plist file, located in /ios/Runner/Info.plist:

NSPhotoLibraryUsageDescription - describe why your app needs permission for the photo library. This is called Privacy - Photo Library Usage Description in the visual editor.

NSCameraUsageDescription - describe why your app needs access to the camera. This is called Privacy - Camera Usage Description in the visual editor.

dependencies: image_picker: ^0.8.4+4

import 'package:image_picker/image_picker.dart';

  final picker = ImagePicker();

Future pickImage() async {
setState(() {
  picselected1 = false;
});
ImagePicker picker = ImagePicker();
PickedFile pickedFile;
pickedFile = await picker.getImage(
  source: ImageSource.gallery,
);

setState(() {
  if (pickedFile != null) {
    picselected1 = true;
    // _images.add(File(pickedFile.path));
    _image = File(pickedFile.path); // Use if you only need a single picture
  } else {
    print('No image selected.');
  }
});
}

Upvotes: 0

Zeeshan Akhtar
Zeeshan Akhtar

Reputation: 521

In my case, I update my Image picker library from 0.6.3 to 0.8.4 hope this helps anyone.

Upvotes: 0

Meet Shah
Meet Shah

Reputation: 82

 File _image;
 String _image1 = "";

 Future getImage() async {

    final pickedFile = await picker.getImage(source: ImageSource.gallery);
    setState(() {
      if (pickedFile != null) {
        _image1 = pickedFile.path;
        _image = File(pickedFile.path);
        print(json.encode(_image1));
        print("file path...");
      } else {
        print('No image selected.');
      }
    });
  }

Upvotes: 2

Bach
Bach

Reputation: 3326

For the iOS, as stated in the documentation, you'll need some config in the native side relating to permission:

Add the following keys to your Info.plist file, located in /ios/Runner/Info.plist:

  • NSPhotoLibraryUsageDescription - describe why your app needs permission for the photo library. This is called Privacy - Photo Library Usage Description in the visual editor.
  • NSCameraUsageDescription - describe why your app needs access to the camera. This is called Privacy - Camera Usage Description in the visual editor.
  • NSMicrophoneUsageDescription - describe why your app needs access to the microphone, if you intend to record videos. This is called Privacy - Microphone Usage Description in the visual editor.

Other than that, it should work perfectly fine with your code. The image should be fit within a Flexible like this, or maybe a SizedBox to avoid overflowing:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(MaterialApp(
    home: SampleScreen(),
  ));
}

class SampleScreen extends StatefulWidget {
  @override
  _SampleScreenState createState() => _SampleScreenState();
}

class _SampleScreenState extends State<SampleScreen> {
  File _image;
  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FlatButton(onPressed: () => getImage(), child: Text('Press me')),
            Flexible(child: _image != null ? Image.file(_image) : Text('no Image')),
          ],
        ),
      ),
    );
  }
}

Upvotes: 2

Related Questions