3
Future getImage(ImageSource imageSource) async {
    var image = await ImagePicker.pickImage(source: imageSource);

    setState(() {
      _image = image;
    });
  } 
1
  • I'm using above code to pick image,but i want it save temporary in my image view at sqlite Commented Mar 20, 2019 at 9:28

3 Answers 3

7

You can convert the image to byte array and save it sqlite as a blob.

var image = await ImagePicker.pickImage(source: imageSource);
List<int> bytes = await image.readAsBytes();
//save to SQLite as a blob
Sign up to request clarification or add additional context in comments.

3 Comments

if you have source code to get image and save it sqlite.if you are use that BLOB file type?. please let me know the creating database and using method for that.
what is the type of sqlite for image? I mean CREATE TABLE(image ?)
how to convert to File again?
3

I got the solution in my question. I'm getting the image from an image_picker and Encode it to BASE64 string value like below

 Uint8List _bytesImage;   
 File _image;
 String  base64Image;

Future getImage() async {
     var image2 = await ImagePicker.pickImage(
      source: ImageSource.gallery,

      );
    List<int> imageBytes = image2.readAsBytesSync();
    print(imageBytes);
    base64Image = base64Encode(imageBytes);
    print('string is');
    print(base64Image);
    print("You selected gallery image : " + image2.path);

    _bytesImage = Base64Decoder().convert(base64Image);

    setState(() {

      _image=image2;

      });
}

after creating an SQLite database dbhelper.dart file to retrieve String values and database model file Image.dart for the get and set the String values.

image.dart

class Image{

  int id;
  String image;


  Employee(this.id, this.image);

   Employee.fromMap(Map map) {
    id= map[id];
    image = map[image];

  }

}

save into database

dbhelper.dart

class DBHelper {
  static Database _db;

  Future<Database> get db async {
    if (_db != null) return _db;
    _db = await initDb();
    return _db;
  }

  initDb() async {
    io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, "test.db");
    var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return theDb;
  }

  void _onCreate(Database db, int version) async {
    // When creating the db, create the table
    await db.execute(
        "CREATE TABLE Imagedata(id INTEGER PRIMARY KEY, image TEXT)");
    print("Created tables");
  }

  void saveImage(Imagedata imagedata) async {
    var dbClient = await db;
    await dbClient.transaction((txn) async {
      return await txn.rawInsert(
          'INSERT INTO Imagedata(id, image) VALUES(' +
              '\'' +
              imagedata.id+
              '\'' +
              ',' +
              '\'' +
              imagedata.image +
              '\'' +
              ')');
    });
  }

  Future<List<Imagedata>> getMyImage() async {
    var dbClient = await db;
    List<Map> list = await dbClient.rawQuery('SELECT * FROM Imagedata');
    List<Imagedata> images= new List();
    for (int i = 0; i < list.length; i++) {
      images.add(new Imagedata(list[i]["id"], list[i]["image"]));
    }
    print(images.length);
    return images;
  }

   Future<int> deleteMyImage(Imagedata imagedata) async {
    var dbClient = await db;

    int res =
        await dbClient.rawDelete('DELETE * FROM Imagedata');
    return res;
  }
}

3 Comments

after saving the image, how to convert to File?
_bytesImage = Base64Decoder().convert(studentImage); use that
stackoverflow.com/questions/55507861/… try this link, it is my another answer for my own question, it provides a better solution in a step by step
0
ImagePicker()
    .pickImage(source: ImageSource.gallery)
    .then((imgFile) async {
                      file = io.File(imgFile!.path.toString());
                      imageUrl =
                          Utility.base64String(await imgFile.readAsBytes());
}

After pic image store in to string 
SQFLITE CREATE TABLE QUERY

await db.execute(
    'CREATE TABLE $TABLE ($ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, $NAME TEXT, $PRIORITY INTEGER, $PHOTO TEXT)');

    Insert Query

    Future<SqfLiteModel> save(SqfLiteModel model) async 
      {
         var dbClient = await db;
         await dbClient!.insert(TABLE, model.toMap()).then((value) => {});
         return model;
      }
    
    
       SqfLiteModel(
          id,
         {required this.name, 
          required this.priority, 
          required this.photo}
        );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.