Storing thumnail to SqlLite from Flutter App

Viewed 71

Here i want to store the thumbnail to My database which is sqlite, and i am not able to store it. i am able to store it in local directory but. i wana call it from my data model. please help
Here is my Code.

   
  _getThumb(videoPathUrl) async {
    var appDocDir = await getApplicationDocumentsDirectory();
    final folderPath = appDocDir.path;
    print(folderPath);
    String thumb = await Thumbnails.getThumbnail(
      thumbnailFolder: folderPath,
      videoFile: videoPathUrl,
      imageType: ThumbFormat.JPEG,
      quality: 25,
    );
    print('thumbnail: $thumb');
    return thumb = model.thumbnail;
  }

}

1 Answers

I Have Found a Solution "the Problem is not with the plugin or the method it adopts to store the Image" The problem is that it is not clear that how should a Unit8List is to be stored in the database.

In My case. __

I am Using this code to Store the Image.

_getThumb(videoPathUrl) async {
    final thumb = await VideoThumbnail.thumbnailData(
      video: videoPathUrl,
      imageFormat: ImageFormat.JPEG,
      maxWidth: 100,
      maxHeight: 100,
      quality: 75,
    );
 }

Then I am Using this final thumb to be saved against my VideoModel Variable of thumbnail

_getThumb(videoPathUrl) async {
    final thumb = await VideoThumbnail.thumbnailData(
      video: videoPathUrl,
      imageFormat: ImageFormat.JPEG,
      maxWidth: 100,
      maxHeight: 100,
      quality: 75,
    );
    print('thumbnail $thumb'); //Add these lines
    model.thumbnail = thumb; //Add these Lines
    return thumb; //Add these lines
  }

And Once i have saved it i can see the print result in my debug console. Once i have that i am sure that it is saved.

Now i have to save it as a Blob in my db not as a String. and thumbnail should be a Unit8List variable not a String Variable.

See the exampleenter image description here

and in database enter image description here

I am Also adding the full Video Model and DbHelper files for Others to use.

VideoModel.dart

import 'dart:typed_data';
import 'model.dart';

class VideoModel extends Model {
  static String table = 'videos';

  int id;
  String videoName;
  String video;
  Uint8List thumbnail;

  VideoModel({
    this.id,
    this.videoName,
    this.video,
    this.thumbnail,
  });

  static VideoModel fromMap(Map<String, dynamic> map) {
    return VideoModel(
      id: map["id"],
      videoName: map['videoName'].toString(),
      video: map['video'],
      thumbnail: map['thumbnail'],
    );
  }

  Map<String, dynamic> toMap() {
    Map<String, dynamic> map = {
      'id': id,
      'videoName': videoName,
      'video': video,
      'thumbnail': thumbnail,
    };

    if (id != null) {
      map['id'] = id;
    }
    return map;
  }
}

DbHelper.dart

abstract class DB {
  static Database _db;

  static int get _version => 1;

  static Future<void> init() async {
    if (_db != null) {
      return;
    }

    try {
      var databasesPath = await getDatabasesPath();
      String _path = p.join(databasesPath, 'video.db');
      _db = await openDatabase(_path, version: _version, onCreate: onCreate);
    } catch (ex) {
      print(ex);
    }
  }

  static void onCreate(Database db, int version) async {
    await db.execute(
        'CREATE TABLE videos (id INTEGER PRIMARY KEY AUTOINCREMENT, videoName STRING, video STRING, thumbnail BLOB)');
  }

  static Future<List<Map<String, dynamic>>> query(String table) async =>
      _db.query(table);

  static Future<int> insert(String table, Model model) async =>
      await _db.insert(table, model.toMap());

  static Future<int> update(String table, Model model) async => await _db
      .update(table, model.toMap(), where: 'id = ?', whereArgs: [model.id]);

  static Future<int> delete(String table, Model model) async =>
      await _db.delete(table, where: 'id = ?', whereArgs: [model.id]);

  static Future<Batch> batch() async => _db.batch();
}

Now when you have done these edits You can just all your image using

Image.memory(add your Image Here);

see example: enter image description here

I hope that this will help some of you if you need help Let me know. Thanks

Related