如何在flutter中检索sqlite数据库中的图像数据?

编程入门 行业动态 更新时间:2024-10-25 16:28:31
本文介绍了如何在flutter中检索sqlite数据库中的图像数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用sqlite检索图像数据。我使用下面的代码

I want to retrieve image data in sqlite. im using below code

var image = await ImagePicker.pickImage(source: imageSource); List<int> bytes = await image.readAsBytes();

我想拍摄图像并保存后sqlite.if是否可以从sqlite数据库中获取并设置图像?

i want to take image and after save it sqlite.if can get and set image from sqlite database ?.

推荐答案

我在问题中找到了解决方案。 我正在从image_picker获取图像,并将其编码为如下所示的BASE64字符串值

I found 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; }); }

创建一个SQLite数据库 dbhelper.dart 文件以检索字符串值,并使用数据库模型文件 Image.dart 获取并设置字符串值。

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

image.dart

class Image{ int id; String image; Employee(this.id, this.image); Employee.fromMap(Map map) { id= map[id]; image = map[image]; } }

dbhelper.dart

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; } }

最后从数据库获取String值并解码String值

last getting String value from the database and Decode String value to the Image file.

从数据库中获取图像

Future<List<Employee>> fetchImageFromDatabase() async { var dbHelper = DBHelper(); Future<List<Imagedata>> images= dbHelper.getImages(); return images; }

在将字符串值解码到图像文件之后

after Decode string value to the Image file

String DecoImage; Uint8List _bytesImage; FutureBuilder<List<Imagedata>>( future: fetchImageFromDatabase(), builder: (context, snapshot) { if (snapshot.hasData) { return new ListView.builder( itemCount: snapshot.data.length, itemBuilder: (context, index) { DecoImage=snapshot.data[index].image; _bytesImage = Base64Decoder().convert(DecoImage); return new SingleChildScrollView( child: Container( child: _bytesImage == null ? new Text('No image value.') : Image.memory(_bytesImage) ), ); } ); } } ),

我认为这对其他开发者,sqlite开发人员很有帮助

i think that is helpful for other flutter,sqlite developers

更多推荐

如何在flutter中检索sqlite数据库中的图像数据?

本文发布于:2023-10-07 10:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   图像   数据   如何在   sqlite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!