Image_picker not giving real path of the image in flutter

596

Well, if you need the image later, you need to copy it and save it somewhere. The actual image, not just the path. If I select an image in your app, I want it to be saved. I don't want it to be gone when I delete the picture from my gallery, or switch phones.

The path you have is sufficient to read the image and save it wherever you want. Your backend most likely, since I want to have my picture, even on another phone.

How you do this is highly dependent on your specific backend, but I'm sure there will be a tutorial for it.

Share:
596
c49
Author by

c49

Updated on December 24, 2022

Comments

  • c49
    c49 over 1 year

    I want to select image from gallery, but when I tried to save it in shared preferences. I found that image_picker gives temporary location like tmp/image_picker_4415467867A964-791E-4AFA995BA-18295-0003861F9255294A.jpg

    This is not real path of the image. How should I get original location path of the image for later use?

    Or I want to save the whole image in database.. what to do? pickimage() is deprecated now?

    Please help

    import 'dart:io';
    import 'package:flutter/material.dart';
    import 'package:image_picker/image_picker.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    void main() {
    runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
     @override
     Widget build(BuildContext context) {
      return MaterialApp(
         title: 'Fluter demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
     }
     }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      File _image;
      String _imageloc;
    
      @override
      void initState() {
        super.initState();
        LoadImage();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Image Picker'),
          ),
          body: Container(
            alignment: Alignment.center,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                _imageloc != null
                    ? CircleAvatar(
                    backgroundImage: FileImage(File(_imageloc)),
                    radius: 80,
                  )
                : CircleAvatar(
                    backgroundImage: _image != null
                        ? FileImage(_image)
                        : NetworkImage(
                            'https://www.publicdomainpictures.net/pictures/320000/velka/background-image.png'),
                  ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                onPressed: () {
                  PickImage();
                },
                child: Text('Pick Image'),
              ),
            ),
            RaisedButton(
              onPressed: () {
                saveImage(_image.path);
              },
              child: Text('saved'),
            ),
          ],
        ),
      ),
    );
      }
    
      void PickImage() async {
        var image = await ImagePicker.pickImage(source: ImageSource.gallery);
    print(image.path);
    setState(() {
      _image = image;
    });
      }
    
      void saveImage(_imageloc) async {
        SharedPreferences preferences = await SharedPreferences.getInstance();
        setState(() {
          preferences.setString('imageloc', _imageloc);
        });
      }
    
      void LoadImage() async {
        SharedPreferences saveimage = await SharedPreferences.getInstance();
        setState(() {
          saveimage.getString('imageloc');
        });
      }
    }
    
    • GamingFelix
      GamingFelix over 3 years
      You probably should write more of your code. Like where are you running the code? What have you tried so far and so on. With code examples-
    • c49
      c49 over 3 years
      Thanks for your reply.. I will post it later.. my system not working now
    • c49
      c49 over 3 years
      I have posted my code
  • c49
    c49 over 3 years
    Thanks.. but what if I want to save it later and at the same time, I want to show it to user?
  • c49
    c49 over 3 years
    Show temporary file or copied file?
  • nvoigt
    nvoigt over 3 years
    You can show it to the user based on the temporary filename, but once they hit save, I'd upload it somewhere.
  • c49
    c49 over 3 years
    Save image in database (format?) or copy it and save into filesystem. Which is better?
  • nvoigt
    nvoigt over 3 years
    That depends on what you want to do with it. If you want to show it later I guess saving it to a filesystem that is served by a webserver might make sense. If it's sensitive information, maybe putting it into a secured database is better. Copying it to the local device is probably not what your users expect from the app.
  • c49
    c49 over 3 years
    I just want to show image when they selected and save it for later. But only in their device and that's not a sensitive information.