skip to Main Content

I need to get all the images from a gallery without using image_picker or other packages in flutter.

I tried using image_picker. But it turned out that it cannot be used. And how after receiving the images to display a preview of the selected image?

2

Answers


  1. You can look at how image_picker works under the hood, and inspect its native implementation to determine the native methods you may be able to use.

    https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android/android/src/main/java/io/flutter/plugins/imagepicker

    See this page on how to run native code in your Flutter app (which you can also do without using a package):
    https://docs.flutter.dev/platform-integration/platform-channels

    The question is, why are you unable to use packages? They should be easy to include via pub.dev or even as a local dependency in your pubspec.yaml if the former does not work.

    To show the preview, I know image_picker copies the file to the app’s temporary directory (which you can get using getTemporaryDirectory() with path_provider), after which you can show an Image.file in your app as the preview.

    Login or Signup to reply.
  2. Yes you can do it. look at bellow code segment.

    1.First you should make varible As File type.

    File? _imageFile;
    

    2.Then you should make a method for select image.like bellow,

      Future<void> _selectImageFromGallery() async {
        final picker = ImagePicker();
        final pickedImage = await picker.pickImage(source: ImageSource.gallery);
    
        if (pickedImage != null) {
          setState(() {
            _imageFile = File(pickedImage.path);
          });
        }
      }
    

    3.Now you can access images like bellow code.

      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Select Image from Gallery'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                _imageFile != null
                    ? Image.file(
                        _imageFile!,
                        width: 200,
                        height: 200,
                      )
                    : Text('No image selected'),
                SizedBox(height: 20),
                ElevatedButton(
                  onPressed: _selectImageFromGallery,
                  child: Text('Select Image'),
                ),
              ],
            ),
          ),
        );
      }
    

    Full code part

    class UploadImage extends StatefulWidget {
      const UploadImage({super.key});
    
      @override
      State<UploadImage> createState() => _UploadImageState();
    }
    
        class _UploadImageState extends State<UploadImage> {
          
        
          File? _imageFile;
        
          Future<void> _selectImageFromGallery() async {
            final picker = ImagePicker();
            final pickedImage = await picker.pickImage(source: ImageSource.gallery);
        
            if (pickedImage != null) {
              setState(() {
                _imageFile = File(pickedImage.path);
              });
            }
          }
        
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: Text('Select Image from Gallery'),
              ),
              body: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    _imageFile != null
                        ? Image.file(
                            _imageFile!,
                            width: 200,
                            height: 200,
                          )
                        : Text('No image selected'),
                    SizedBox(height: 20),
                    ElevatedButton(
                      onPressed: _selectImageFromGallery,
                      child: Text('Select Image'),
                    ),
                  ],
                ),
              ),
            );
        
          }
          }
    

    **If you want to use filepicker try bellow steps,

    1.You should add some packages before use this method,

      file_picker: ^5.5.0
    

    you can get it by using this link

    now you can import it like this import 'package:file_picker/file_picker.dart';

    import 'package:file_picker/file_picker.dart';
    import 'package:flutter/material.dart';
    
        class UploadImage extends StatefulWidget {
          const UploadImage({super.key});
        
          @override
          State<UploadImage> createState() => _UploadImageState();
        }
        
        class _UploadImageState extends State<UploadImage> {
          File? file;
        
          @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: const Text('Upload Image'),
              ),
              body: SingleChildScrollView(
                child: Center(
                  child: Column(
                    children: [
                      file != null
                          ? Image.file(file!)
                          : Image.asset('assets/images/defult.png'),
                      TextButton(
                        onPressed: () {
                          pickFile();
                        },
                        child: const Text('Pick a file'),
                      ),
                    ],
                  ),
                ),
              ),
            );
          }
        
          pickFile() async {
            FilePickerResult? result =
                await FilePicker.platform.pickFiles(type: FileType.image);
        
            if (result != null) {
              setState(() {
                file = File(result.files.single.path ?? "");
              });
            }
          }
    

    try like this you can now fix you’r issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search